Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
Forked from ericelliott/create-vs-new.js
Created October 20, 2015 13:03
Show Gist options
  • Save manjeettahkur/0746c23a2fd70bf3771f to your computer and use it in GitHub Desktop.
Save manjeettahkur/0746c23a2fd70bf3771f to your computer and use it in GitHub Desktop.
/* Object.create() vs new Speed Test
* @author Eric Hamilton
*
* Result: For a few thousand objects, it
* doesn't matter which way you go.
* For hundreds of thousands of objects,
* you might want to use new instead of
* Object.create().
*
* Tested on node v0.2.0
* 50,000 objects:
* new MyObject(): 10-20ms
* Object.create(proto): 40-50ms
*/
"use strict";
var test = {
configure : {
times : 50000
},
Constructor : function () {
this.a = 'a';
this.b = 'b';
this.c = 'c';
this.d = 'd';
this.e = 'e';
this.f = 'yay, F!';
this.g = "It's a mutha flippin' G thang";
this.h = 24;
this.i = false;
this.j = true;
this.k = 'special k';
},
myProto : {
a : 'a',
b : 'b',
c : 'c',
d : 'd',
e : 'e',
f : 'yay, F!',
g : "It's a mutha flippin' G thang",
h : 24,
i : false,
j : true,
k : 'special k'
},
compare : {
constructor : {
name : 'Constructor',
fn : function () {
var obj = new test.Constructor();
}
},
create : {
name : 'Create',
fn : function () {
var obj = Object.create(test.myProto);
}
}
}
};
(function () {
var results,
key,
compare = test.compare,
times = +test.configure.times,
start;
console.log('Running ' + times + ' times:');
for (key in compare) {
if (compare.hasOwnProperty(key)) {
times = +test.configure.times;
start = new Date();
while (times-- > 0) {
compare[key].fn();
}
console.log((new Date() - start) + ' ms: ' + compare[key].name);
}
}
return results;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment