Skip to content

Instantly share code, notes, and snippets.

@zloyrusskiy
Created August 23, 2016 13:16
Show Gist options
  • Save zloyrusskiy/b725dd2ca325d941ad6d2c7330979ad9 to your computer and use it in GitHub Desktop.
Save zloyrusskiy/b725dd2ca325d941ad6d2c7330979ad9 to your computer and use it in GitHub Desktop.
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
// add tests
suite.add('lowercase', function() {
let input = 'ai';
let dictionary = ['airplane','airport','apple','ball'];
var clean_str = input.replace(/[^a-z]+/gi,'').toLowerCase();
let res = dictionary
.filter(x => x.toLowerCase().startsWith(clean_str))
.slice(0,5);
})
.add('regex', function() {
let input = 'ai';
let dictionary = ['airplane','airport','apple','ball'];
var r = new RegExp('^' + input.replace(/[^a-z]/gi,''), 'i');
let res = dictionary.filter(function(w){ return r.test(w); }).slice(0, 5);
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log(this);
})
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment