Skip to content

Instantly share code, notes, and snippets.

@79yuuki
Created May 8, 2013 06:52
Show Gist options
  • Save 79yuuki/5538713 to your computer and use it in GitHub Desktop.
Save 79yuuki/5538713 to your computer and use it in GitHub Desktop.
Performance verification of indexOf & RegExp of JavaScript
var a = [], n=10000;
for(var i=0; i<n; i++){
a.push('item:' + i);}
var str = a.join('\n');
// test1
var s = new Date();
for(var i=0; i<3000; i++){
var m = str.indexOf('item:9999');
}
console.log('#test1 ' + (new Date() - s) + '(ms) result:'+ m);
// test2
var s = new Date();
for(var i=0; i<3000; i++){
var m = /item:9999/.exec(str);
}
console.log('#test2 ' + (new Date() - s) + '(ms) result:'+ m);
// test3
var s = new Date();
for(var i=0; i<3000; i++){
var m = new RegExp('item:9999').exec(str);
}
console.log('#test3 ' + (new Date() - s) + '(ms) result:'+ m);
// test4
var s = new Date();
for(var i=0; i<3000; i++){
var m = /item:9999$/.exec(str);
}
console.log('#test4 ' + (new Date() - s) + '(ms) result:'+ m);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment