Large performance variance with the size and methods of creating a JavaScript object.

I noticed a significant performance difference between the various methods of creating objects. Moreover, it seems that the size of the object (i.e. the number of properties) sometimes matters.

Can anyone shed some light on the results of the next jsperf? http://jsperf.com/objects-w-more-less-8-properties/5

var thisobj = {a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8} 

It’s like squeezing the set way to create a new object

var thisobj = {a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9}

This is much slower ... The only difference is the 9th value

var thisobj = new objsm(1,2,3,4,5,6,7,8); 

AND

var thisobj = new objlg(1,2,3,4,5,6,7,8,9);

Do not differ much from each other (about the same amount you expect), but they are still very different from the “dynamically” defined object above. Their objects are defined here:

    var objsm = function (a,b,c,d,e,f,g,h) {
    this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; this.g = g; this.h = h;}

var objlg = function (a,b,c,d,e,f,g,h,i) {
this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; this.g = g; this.h = h; this.i = i; }

Why is " var thisobj = {a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8}" so excellent?

+4
2

, - javascript, - .

, (, , GC), .

, , , .

, FF 45 :

FF45 Performance Test

, JIT- , , . , , , .

, :

enter image description here

, ™ , , , , , .

"" .

Microbenchmarks - .

+1

, , , , , . , .

8-? HashMap , , ( , ). - ( 8-10), 8 .

, main jsperf , , : , .

0

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


All Articles