Skip to content

Instantly share code, notes, and snippets.

@kevinohara80
Created December 22, 2011 18:47
Show Gist options
  • Save kevinohara80/1511377 to your computer and use it in GitHub Desktop.
Save kevinohara80/1511377 to your computer and use it in GitHub Desktop.
Javascript eval() benchmark
/* Test Eval */
var eval_string = 'var x = 1;'
+ 'if(x >= 1) {'
+ ' var y = x * 2;'
+ ' if(y==3) { y=3; }'
+ '}';
var eval_start = (new Date()).getTime();
for(var i=0; i<1000000; i++) {
eval(eval_string);
}
var eval_end = (new Date()).getTime();
var eval_diff = (eval_end - eval_start);
console.log('eval time -> ' + (eval_diff/1000) + ' seconds');
/* Test normal js */
var js_start = (new Date()).getTime();
for(var i=0; i<1000000; i++) {
var x = 1;
if(x >= 1) {
var y = x * 2;
if(y==3) { y=4; }
}
}
var js_end = (new Date()).getTime();
var js_diff = (js_end - js_start);
console.log('js time -> ' + (js_diff/1000) + ' seconds');
node benchmark.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment