Skip to content

Instantly share code, notes, and snippets.

@piglovesyou
Created December 8, 2012 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piglovesyou/4239149 to your computer and use it in GitHub Desktop.
Save piglovesyou/4239149 to your computer and use it in GitHub Desktop.
Create literal vs use static
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var obj = {
yeah: 'yeah',
ohh: 'ohh'
};
var arr = [
'yeah',
'ohh'
];
suite.add('Object Literal', function() {
var o = {
yeah: 'yeah',
ohh: 'ohh'
};
o.yeah;
}).add('Object Static', function() {
var o = obj;
o.yeah;
}).add('Array Literal', function() {
var a = [
'yeah',
'ohh'
];
a[1];
}).add('Array Static', function() {
var a = arr;
a[1];
})
.on('cycle', function(event) {
console.log(event.target.toString());
}).on('complete', function() {
console.log('----Fastest is ' + this.filter('fastest').pluck('name'));
}).run({ 'async': true });
@piglovesyou
Copy link
Author

$ node literal.js 
Object Literal x 154,344,750 ops/sec ±8.88% (72 runs sampled)
Object Static x 78,987,605 ops/sec ±1.19% (89 runs sampled)
Array Literal x 152,237,669 ops/sec ±9.32% (71 runs sampled)
Array Static x 74,883,876 ops/sec ±0.83% (91 runs sampled)
----Fastest is Object Literal

$ node -v
v0.8.14
$ uname -a
Darwin soichi-macminit.local 12.2.1 Darwin Kernel Version 12.2.1: Thu Oct 18 12:13:47 PDT 2012; root:xnu-2050.20.9~1/RELEASE_X86_64 x86_64

Mac OSX 10.0.2, 2.3GHz Intel Core i7, 4GB 1600 MHz DDR 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment