Skip to content

Instantly share code, notes, and snippets.

@tiagosr
Created February 4, 2011 21:53
Show Gist options
  • Save tiagosr/811852 to your computer and use it in GitHub Desktop.
Save tiagosr/811852 to your computer and use it in GitHub Desktop.
A quick test for javascript object method declaration "modes".
function TestObj1() { this.counter = 0; this.doit = function() { return this.counter++ } };
function TestObj2() { this.counter = 0; };
TestObj2.prototype.doit = function() { return this.counter++; };
testit = function() { var testobj1 = new TestObj1(); var counter = 0; var startt1 = (new Date).getTime(); while(testobj1.doit()<1000000) { counter++ }; console.log("time for test 1: "+((new Date).getTime()-startt1)); var testobj2 = new TestObj2(); var startt2 = (new Date).getTime(); while(testobj2.doit()<1000000) { counter++ }; console.log("time for test 2: "+((new Date).getTime()-startt2));};
testit();
// Firefox 3.6 is awful slow on both methods, but the first one is a tiny bit faster than the second one (on winXP 32, AMD x2 2.2GHz 4GB ram, it does 522ms vs. 534ms).
// Firefox 4.0 is some sort of improvement, but is still godawful slow. 340ms and 350ms, respectively. I guess Tamarin does not help much on this.
// Chrome Canary Build flies on both methods, and the second one is almost 2x faster - 45ms vs. 27ms, consistently.
// Safari 5, though, stands in the middle ground on the first method, doing 127ms, but actually reaches Chrome's speed on the second test, doing 27ms too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment