Created
September 4, 2018 02:46
-
-
Save xixilive/99f4dc4dc175ca0386cb6e34d5f24ac1 to your computer and use it in GitHub Desktop.
Performance testing of a javascript RegExp matching
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
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