Will the JS runtime use hidden classes for an object literal?

Thanks Mr. Alef and their wonderful blog , we know that JS battery life can generate memory-efficient structures for ordinary sequential classes. For example, with a class:

class Foo {
  constructor(bar: number) {
    this.bar = bar;
  }
}

I understand that the runtime can highlight struct Foo { double bar; }when you are new Foo(3.14). This is done in order to make access faster and use less memory, but applies only to compatible classes. Usually this means the results of a constructor that stores returned objects with the same properties.

What happens when you use an object literal instead of a constructor to create objects? For instance:

const arr = [];
for (i = 0; i < a_whole_bunch; ++i) {
  arr.push(new Foo(Math.random()));
}

// vs

const arr = [];
for (i = 0; i < a_whole_bunch; ++i) {
  arr.push({bar: Math.random()});
}

, , . , .

?

+4

Source: https://habr.com/ru/post/1656989/


All Articles