Last active
December 14, 2015 23:59
-
-
Save dai-shi/5169296 to your computer and use it in GitHub Desktop.
benchmark regex.test, string.match, and string.search
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() { | |
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 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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