Skip to content

Instantly share code, notes, and snippets.

@xixilive
Created September 4, 2018 02:46
Show Gist options
  • Save xixilive/99f4dc4dc175ca0386cb6e34d5f24ac1 to your computer and use it in GitHub Desktop.
Save xixilive/99f4dc4dc175ca0386cb6e34d5f24ac1 to your computer and use it in GitHub Desktop.
Performance testing of a javascript RegExp matching
function strip1(name){
return name.replace(/^(\/*)|(\/*)$/g, '')
}
function strip2(name){
return name.replace(/^(\/+)|(\/+)$/g, '')
}
let str = "///packages/comm/packa/packages/packages/commocomm/packages/"
function measure(fn) {
var tag = "Time of " + fn.name + ":"
console.time(tag)
for (let i = 0; i < 1000000; i++) {
fn(str)
}
console.timeEnd(tag)
}
console.log('\nStrip with wildcard matching\n')
measure(strip1)
console.log('\nStrip with minimal matching\n')
measure(strip2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment