Skip to content

Instantly share code, notes, and snippets.

@dai-shi
Created March 11, 2013 10:57
Show Gist options
  • Save dai-shi/5133447 to your computer and use it in GitHub Desktop.
Save dai-shi/5133447 to your computer and use it in GitHub Desktop.
benchmark of String.lastIndexOf in Node.js
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();
};
var suite = new Benchmark.Suite();
// add tests
suite.add('existence', function() {
var r1 = (s.lastIndexOf("test") === 0);
})
.add('nonexistence', function() {
var r2 = (s.lastIndexOf("not there") === 0);
})
.add('existence start 0', function() {
var r1 = (s.lastIndexOf("test", 0) === 0);
})
.add('nonexistence start 0', function() {
var r2 = (s.lastIndexOf("not there", 0) === 0);
})
.add('existence indexOf', function() {
var r1 = (s.indexOf("test") === 0);
})
.add('nonexistence indexOf', function() {
var r2 = (s.indexOf("not there") === 0);
})
// 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
});
@dai-shi
Copy link
Author

dai-shi commented Mar 11, 2013

existence x 2,044 ops/sec ±0.98% (78 runs sampled)
nonexistence x 6,596 ops/sec ±0.38% (84 runs sampled)
existence start 0 x 10,002,925 ops/sec ±0.64% (85 runs sampled)
nonexistence start 0 x 10,531,223 ops/sec ±0.73% (93 runs sampled)
existence indexOf x 10,945,037 ops/sec ±0.48% (89 runs sampled)
nonexistence indexOf x 11,610 ops/sec ±13.01% (66 runs sampled)
Fastest is existence indexOf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment