What are the differences between Object.freeze () and Object.seal ()?

3

I know that 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 I 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"]);  

Questions

  • Are there differences between Object.freeze() and Object.seal() ? If yes, which ones?
  • When I freeze an object with one of two methods do I have an increase in performance?
  • What is the browser support for methods?
  • asked by anonymous 23.10.2018 / 22:22

    1 answer

    4
      

    Are there any differences between Object.freeze () and Object.seal ()? If so, which ones?

    seal() places a meta property in the object which indicates which properties can not be added or deleted. Remembering that objects in JS in the background are just data dictionaries, so just add or remove an element in the structure.

    freeze() also marks the properties can not be changed, which makes the immutable object, its status 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"]);
      

    When I freeze an object with one of two methods do I have a performance boost?

    It should, but interestingly does not affect much, and may even get worse, as it actually happens or has already happened. I will not go into detail because this depends on implementation, it may be one thing in the current version, but change the next, so a response that says it is slower or faster may be out of date in a month. I do not rule out the engines that are currently sophisticated that they can optimize even without this guarantee in some cases where it realizes that there will be no problems. So I would not rely on superficial testing of those who do not understand where it can make a difference (people often make 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, especially in a competing environment, since it is guaranteed that the data will not be affected. p>

      

    How do browsers support the methods?

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

        
    23.10.2018 / 22:41