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?