Most Asked Interview Question -Object.freeze() VS Object.seal()

Deepa Jarout
2 min readJan 1, 2023

--

difference between object.freeze() and object.seal() by deepajarout

In JavaScript, both freeze and seal are used to construct non-extendable objects, but they differ in several ways.
When compared to Object.freeze(), Object.seal() permits modifications to an object’s current properties.

Let’s dive right into the topic and compare and know when to use which function and why.

Object.freeze()

An object becomes impervious to all modifications when it uses the Object.freeze() function.

It prevents properties from being added to or removed from the object; calling delete will return false.

Trying to change the value of the sealed object itself may result in a TypeError (most commonly in strict mode)

It prohibits the modification of any current properties.

Its return type is of the object type.

object.freeze() by deepajarout
object.freeze() by deepajarout-(figure a)

Figure (a), demonstrates how to build a non-extensible object using the Object.freeze() method, with the output of “Deepa Jarout” and a restriction on changing the object’s current value.

Object.seal()

Existing properties are protected against deletion by Object.seal(), but they are not shielded from modifications from the outside world.

It stops the sealed object from having any additional or removed attributes, and invoking delete will return false.

It prevents the conversion of any existing property from “data descriptors” to “accessor descriptors” (and vice versa), and it prevents any attribute of accessor descriptors from being changed at all (whereas data descriptors can change their writable attribute, and their value attribute if writeable is true).

Can cause a TypeError if the value of the sealed object itself is attempted to be changed (most commonly in strict mode)

object.seal() by deepajarout(figure-b)
object.seal() by deepajarout- figure-(b)

Figure(b), illustrates how an extensible object is created using Object.seal(), but it is still possible to change the object’s value, as shown when the name is modified to “Dishika Jarout”.

Performance

Depending on the browser, sealing or freezing an object may impact how quickly it is enumerated:

Firefox: There is no influence on enumeration performance.
I.E., there is little impact on enumeration performance.
With sealed or frozen items, Chrome’s enumeration performance is faster.
In Safari, counting sealed or frozen objects takes 92% longer (as of 2014)

--

--

Deepa Jarout

I am Software developer.I like problem solving, traveling, photography, dancing, video games, conversation.I like to explore new places with different tradition