Skip to content

Instantly share code, notes, and snippets.

@dai-shi
Last active December 14, 2015 23:59
Show Gist options
  • Save dai-shi/5169296 to your computer and use it in GitHub Desktop.
Save dai-shi/5169296 to your computer and use it in GitHub Desktop.
benchmark regex.test, string.match, and string.search
var Benchmark = require('benchmark');
Benchmark.prototype.setup = function() {
s = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxtestyyyyyyyyyyyyyyyyyyyyy';
re = new RegExp('te[xs]t');
};
var suite = new Benchmark.Suite();
// add tests
suite.add('regex test', function() {
var r1 = re.test(s);
})
.add('regex exec', function() {
var r2 = re.exec(s);
})
.add('string match', function() {
var r3 = s.match(re);
})
.add('string search', function() {
var r4 = s.search(re);
})
// 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 15, 2013

regex test x 8,760,875 ops/sec ±0.60% (88 runs sampled)
regex exec x 4,767,018 ops/sec ±5.87% (72 runs sampled)
string match x 5,342,340 ops/sec ±0.39% (98 runs sampled)
string search x 7,306,422 ops/sec ±0.59% (94 runs sampled)
Fastest is regex test

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