Created
August 26, 2010 08:30
-
-
Save ericelliott/551072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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