public
Last active

Performance check about object initialization.

  • Download Gist
bench.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
var before, after;
var obj, i;
var count = 1000000000;
var t;
 
console.log('%j', process.versions);
 
console.log('');
console.log('* initialize');
 
before = new Date;
for (i = 0; i < count; i++)
obj = {};
after = new Date;
 
console.log('{}:\n %d ms', after - before);
 
 
before = new Date;
for (i = 0; i < count; i++) {
obj = {}; obj.__proto__ = null;
}
after = new Date;
 
console.log('obj = {};obj.__proto__ = null:\n %d ms', after - before);
 
console.log('');
console.log('* lookup');
 
obj = {key: true};
before = new Date;
for (i = 0; i < count; i++)
obj.key = true
after = new Date;
 
console.log('obj = {key: true}:\n %d ms', after - before);
 
 
obj = {key: true};
obj.__proto__ = null;
before = new Date;
for (i = 0; i < count; i++)
obj.key = true
after = new Date;
 
console.log('obj = {};obj.__proto__ = null:\n %d ms', after - before);

OS: Mac OS X 10.7.5

{"http_parser":"1.0","node":"0.8.15","v8":"3.11.10.25","ares":"1.7.5-DEV","uv":"0.8","zlib":"1.2.3","openssl":"1.0.0f"}

* initialize
{}:
  5723 ms
obj = {};obj.__proto__ = null:
  5628 ms

* lookup
obj = {key: true}:
  1666 ms
obj = {};obj.__proto__ = null:
  1084 ms

{"http_parser":"1.0","node":"0.8.15","v8":"3.11.10.25","ares":"1.7.5-DEV","uv":"0.8","zlib":"1.2.3","openssl":"1.0.0f"}

  • initialize
    {}:
    5391 ms
    obj = {};obj.proto = null:
    135251 ms

  • lookup
    obj = {key: true}:
    1445 ms
    obj = {};obj.proto = null:
    1308 ms

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.