Created
February 13, 2012 08:16
-
-
Save bjouhier/1814923 to your computer and use it in GitHub Desktop.
Simple bench of basic JS constructs
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
function bench(name, fn) { | |
var result; | |
// 2 passes to let V8 optimize | |
for (var pass = 2; pass >= 0; pass--) { | |
var count = 1; | |
while (true) { | |
var t0 = Date.now(); | |
fn(count); | |
dt = (Date.now() - t0); | |
if (dt > 100) { | |
result = Math.round(dt * 1000 * 1000 / count); | |
break; | |
} | |
count *= 2; | |
} | |
} | |
console.log(name + "\t" + result + "ns"); | |
} | |
var DIGITS = "0123456789"; | |
bench(";", function(count) { | |
for (var i = count; i > 0; i--) {; | |
} | |
}) | |
bench("x++", function(count) { | |
var x = 0; | |
for (var i = count; i > 0; i--) { | |
x++; | |
} | |
}) | |
bench("f()", function(count) { | |
function f() {}; | |
for (var i = count; i > 0; i--) { | |
f(); | |
} | |
}) | |
bench("f(1,2,3)", function(count) { | |
function f() {}; | |
for (var i = count; i > 0; i--) { | |
f(1, 2, 3); | |
} | |
}) | |
bench("f.call(this)", function(count) { | |
function f() {}; | |
for (var i = count; i > 0; i--) { | |
f.call(this); | |
} | |
}) | |
bench("f.apply(this, [])", function(count) { | |
function f() {}; | |
var arr = []; | |
for (var i = count; i > 0; i--) { | |
f.apply(this, arr); | |
} | |
}) | |
bench("{}", function(count) { | |
for (var i = count; i > 0; i--) { | |
var foo = {}; | |
} | |
}) | |
bench("{a:1,b:2,c:3}", function(count) { | |
for (var i = count; i > 0; i--) { | |
var foo = { | |
a: 1, | |
b: 2, | |
c: 3 | |
}; | |
} | |
}) | |
bench("new", function(count) { | |
for (var i = count; i > 0; i--) { | |
var foo = new Object(); | |
} | |
}) | |
bench("[]", function(count) { | |
for (var i = count; i > 0; i--) { | |
var foo = []; | |
} | |
}) | |
bench("[1,2,3]", function(count) { | |
for (var i = count; i > 0; i--) { | |
var foo = [1, 2, 3]; | |
} | |
}) | |
bench("try/catch", function(count) { | |
for (var i = count; i > 0; i--) { | |
try {} catch (ex) {} | |
} | |
}) | |
bench("try/finally", function(count) { | |
for (var i = count; i > 0; i--) { | |
try {} finally {} | |
} | |
}) | |
bench("new Error()", function(count) { | |
for (var i = count; i > 0; i--) { | |
new Error(); | |
} | |
}) | |
Error.stackTraceLimit = 0; | |
bench("new Error() no stack", function(count) { | |
for (var i = count; i > 0; i--) { | |
new Error(); | |
} | |
}) | |
bench("random", function(count) { | |
for (var i = count; i > 0; i--) { | |
Math.random(); | |
} | |
}) | |
bench("exp(2.1)", function(count) { | |
for (var i = count; i > 0; i--) { | |
Math.exp(2.1); | |
} | |
}) | |
bench("s.length", function(count) { | |
for (var i = count; i > 0; i--) { | |
DIGITS.length; | |
} | |
}) | |
bench("s[0]", function(count) { | |
for (var i = count; i > 0; i--) { | |
DIGITS[0]; | |
} | |
}) | |
bench("s.charCodeAt(0)", function(count) { | |
for (var i = count; i > 0; i--) { | |
DIGITS.charCodeAt(0); | |
} | |
}) | |
bench("/\d+/.exec(s)", function(count) { | |
for (var i = count; i > 0; i--) { | |
/\d+/.exec(DIGITS); | |
} | |
}) | |
bench("RE.exec(s)", function(count) { | |
var RE = /\d+/; | |
for (var i = count; i > 0; i--) { | |
RE.exec(DIGITS); | |
} | |
}) | |
bench("RE capture", function(count) { | |
var RE = /(\d+)/; | |
for (var i = count; i > 0; i--) { | |
RE.exec(DIGITS); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment