facebookarchive/prepack

Temporarily Freeze an Object

Open

#586 aberto em 5 de mai. de 2017

Ver no GitHub
 (5 comments) (1 reaction) (0 assignees)JavaScript (520 forks)batch import
enhancementhelp wanted

Métricas do repositório

Stars
 (14.268 stars)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 30d)

Description

Tangentially related to #584. In the React ecosystem we tend to Object.freeze objects only in development mode but not in production because VMs are slower with freeze than with it. However, we do rely on objects not being mutated in production neither because of that.

I'd like to have a way to tell Prepack that an object or individual property is immutable for sake of optimizations and during the runtime of the initialization - but don't serialize it.

var obj1 = { immutable: 0 };
var obj2 = { immutable: 1, mutable: 2 };
if (__PREPACK__) {
  __pseudoFreeze(obj1);
  __pseudoFreezeProperty(obj1, 'immutable');
} else if (process.env.NODE_ENV !== 'production') {
  Object.freeze(obj);
  Object.defineProperty(obj2, 'immutable', { configurable: false, writable: false });
}

->

var obj1 = { immutable: 0 };
var obj2 = { immutable: 1, mutable: 2 };

Guia do colaborador