What are the differences between Object.Freeze() and Object.Seal()?

Asked

Viewed 195 times

3

I know that the Object.freeze() and Object.seal() are used to "freeze" an object. Soon, knowing this, if you run the example below you will see that the two objects were frozen and soon could not assign values to them.

let object1 = {}, object2 = {};

Object.freeze(object1);
Object.seal(object2);

object1["test"] = "valor teste";
object2["test"] = "valor teste";

console.log(object1["test"]);   
console.log(object2["test"]);  

Doubts

  1. There are differences between the Object.freeze() and Object.seal()? If yes, which?
  2. When freezing an object with one of two methods I get a performance boost?
  3. What is the support of browsers regarding methods?

1 answer

4


Are there differences between Object.Freeze() and Object.Seal()? If so, which ones?

The seal() puts a metaproperty on the object indicating that properties cannot be added or deleted. Remembering that JS objects in the background are just data dictionaries, so just add or remove an element in the structure.

The freeze() also that existing properties cannot be changed, which makes the object immutable, its state never changes.

To see better it would be nice to have an existing property and change it in each case to realize the difference:

let object1 = {}, object2 = {};
object2["test1"] = "valor teste";
object1["test1"] = "valor teste";
Object.freeze(object1);
Object.seal(object2);
object1["test"] = "valor teste";
object2["test"] = "valor teste";
object1["test1"] = "mudei";
object2["test1"] = "mudei";
console.log(object1["test"]);
console.log(object1["test1"]);
console.log(object2["test"]);
console.log(object2["test1"]);

I put in the Github for future reference.

When freezing an object with one of two methods I get a performance boost?

It should, but interestingly it doesn’t affect much, and it can even get worse, as it does or has already happened. I will not go into detail because it depends on implementation, it may be one thing in the current version, but change in the next one, so a response that says it is slower or faster may be lagging behind in a month. I don’t rule out the Engines currently being sophisticated that can optimize even without this guarantee in some cases where he realizes that there will be no problems. That’s why I wouldn’t trust superficial tests of people who don’t understand where they can make a difference (people usually do naive tests).

At the moment it is guaranteed that the structure does not change could optimize and use something other than a dictionary and even some other optimizations are possible, mainly in a competing environment, since it is guaranteed that the data will not be affected.

What is the support of browsers regarding methods?

All the widely used patterns have these methods there are many versions so we can say it has universal acceptance.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.