Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between freeze and seal

People also ask

What are the differences between freeze and seal methods?

freeze() makes an object immune to everything even little changes cannot be made. Object. seal() prevents from deletion of existing properties but cannot prevent them from external changes.

What is object freeze?

freeze() The Object. freeze() method freezes an object. Freezing an object prevents extensions and makes existing properties non-writable and non-configurable.

What is the difference between object freeze and Const?

Object. freeze works on values, and more specifically, object values. It makes an object immutable, i.e. you cannot change its properties. Basically, const is the new var ; it's just block-scoped and prevents reassignment.

Why would you freeze an object?

freeze() which is used to freeze an object. Freezing an object does not allow new properties to be added to an object and prevents from removing or altering the existing properties. Object. freeze() preserves the enumerability, configurability, writability and the prototype of the object.


Object.seal

  • It prevents adding and/or removing properties from the sealed object; using delete will return false
  • It makes every existing property non-configurable: they cannot be converted from 'data descriptors' to 'accessor descriptors' (and vice versa), and no attribute of accessor descriptors can be modified at all (whereas data descriptors can change their writable attribute, and their value attribute if writeable is true).
  • Can throw a TypeError when attempting to modify the value of the sealed object itself (most commonly in strict mode)

Object.freeze

  • Exactly what Object.seal does, plus:
  • It prevents modifying any existing properties

Neither one affects 'deep'/grandchildren objects. E.g., if obj is frozen, obj.el can’t be reassigned, but the value of obj.el could be modified, e.g. obj.el.id can be changed.


Performance:

Sealing or freezing an object may affect its enumeration speed, depending on the browser:

  • Firefox: enumeration performance is not impacted
  • IE: enumeration performance impact is negligible
  • Chrome: enumeration performance is faster with sealed or frozen objects
  • Safari: sealed or frozen objects enumerate 92% slower (as of 2014)

Tests: Sealed objects, Frozen objects.


I wrote a test project which compares these 3 methods:

  • Object.freeze()
  • Object.seal()
  • Object.preventExtensions()

My unit tests cover CRUD cases:

  • [C] add new property
  • [R] read existed property
  • [U] modify existed property
  • [D] remove existed property

Result:

enter image description here


You can always looks these up in MDN. In short:

  • Freeze: makes the object immutable, meaning no change to defined property allowed, unless they are objects.
  • Seal: prevent addition of properties, however defined properties still can be changed.

Object.freeze() creates a frozen object, which means it takes an existing object and essentially calls Object.seal() on it, but it also marks all “data accessor” properties as writable:false, so that their values cannot be changed.

-- Kyle Simpson, You Don't Know JS - This & Object Prototypes