Skip to content

Instantly share code, notes, and snippets.

@JosephPecoraro
Created May 9, 2010 22:44
Show Gist options
  • Save JosephPecoraro/395477 to your computer and use it in GitHub Desktop.
Save JosephPecoraro/395477 to your computer and use it in GitHub Desktop.
// So this will run in a browser as well as a shell.
if (this.window) window.print = function(s) { console.log(s); }
var TOTAL = 1e6;
var str = "color: red";
// Test with regex
(function() {
var start = Date.now();
for (var i=0; i<TOTAL; ++i) {
var match = str.match(/[^:]+/);
if (match)
match[0];
}
var end = Date.now();
print(end-start+'ms');
})();
// Test with substring
(function() {
var start = Date.now();
for (var i=0; i<TOTAL; ++i) {
var index = str.indexOf(':');
if (index >= 0)
str.substring(0, str.indexOf(':'));
}
var end = Date.now();
print(end-start+'ms');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment