Skip to content

Instantly share code, notes, and snippets.

@gpedro
Created June 20, 2017 04:12
Show Gist options
  • Save gpedro/684a014f66cf9958498af7fed48442ef to your computer and use it in GitHub Desktop.
Save gpedro/684a014f66cf9958498af7fed48442ef to your computer and use it in GitHub Desktop.
regex-benchmark
RegExp#newInstance x 3,181,722 ops/sec ±1.59% (79 runs sampled)
RegExp#storedInstance x 13,501,235 ops/sec ±1.20% (84 runs sampled)
inline#jit x 7,129,021 ops/sec ±1.07% (82 runs sampled)
inline#stored x 13,703,416 ops/sec ±0.90% (84 runs sampled)
Fastest is inline#stored,RegExp#storedInstance

package.json

{
  "dependencies": {
    "benchmark": "^2.1.4",
    "microtime": "^2.1.5"
  }
}
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var storedInstance = RegExp('/o/');
var storedInline = /o/;
// add tests
suite
.add('RegExp#newInstance', function() {
RegExp('/o/').test('o')
})
.add('RegExp#storedInstance', function() {
storedInstance.test('o')
})
.add('inline#jit', function() {
/o/.test('o')
})
.add('inline#stored', function() {
storedInstance.test('o')
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment