facebookarchive/prepack

Temporarily Freeze an Object

Open

#586 创建于 2017年5月5日

在 GitHub 查看
 (5 评论) (1 反应) (0 负责人)JavaScript (14,268 star) (520 fork)batch import
enhancementhelp wanted

描述

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 };

贡献者指南