Created
February 14, 2013 03:51
-
-
Save dai-shi/4950506 to your computer and use it in GitHub Desktop.
benchmark of String.startsWith equivalents in Node.js
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
var Benchmark = require('benchmark'); | |
Benchmark.prototype.setup = function() { | |
a = ["test"]; | |
for (var i = 0; i < 10000; i++) { | |
a.push("some other stuff"); | |
} | |
s = a.join(); | |
re1 = new RegExp("^test"); | |
re2 = new RegExp("^not there"); | |
}; | |
var suite = new Benchmark.Suite(); | |
// add tests | |
suite.add('indexOf', function() { | |
var r1 = (s.indexOf("test") === 0); | |
var r2 = (s.indexOf("not there") === 0); | |
}) | |
.add('lastIndexOf', function() { | |
var r1 = (s.lastIndexOf("test", 0) === 0); | |
var r2 = (s.lastIndexOf("not there", 0) === 0); | |
}) | |
.add('substring', function() { | |
var r1 = (s.substring(0, "test".length) == "test"); | |
var r2 = (s.substring(0, "not there".length) == "not there"); | |
}) | |
.add('slice', function() { | |
var r1 = (s.slice(0, "test".length) == "test"); | |
var r2 = (s.slice(0, "not there".length) == "not there"); | |
}) | |
.add('regex', function() { | |
var r1 = (/^test/).test(s); | |
var r2 = (/^not there/).test(s); | |
}) | |
.add('compiled regex', function() { | |
var r1 = re1.test(s); | |
var r2 = re2.test(s); | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
// run async | |
.run({ | |
'async': false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run this via command line?
You can use
repeat()
to multiply a stringI had to use simplification method