Skip to content

Instantly share code, notes, and snippets.

@danleyb2
Last active July 15, 2022 07:39
Show Gist options
  • Save danleyb2/b48f8b9a479f8d617961ef5e2641d8ac to your computer and use it in GitHub Desktop.
Save danleyb2/b48f8b9a479f8d617961ef5e2641d8ac to your computer and use it in GitHub Desktop.
Regex Pre-Compilation Performance difference
const RUNS = 100000;
// PRE-COMPILE REGEX
console.time('processRequest1')
const regex = RegExp(/^camera-([0-9]+)/)
function processRequest1(){
let camId = regex.exec('camera-1')
return camId
}
for (let i = 0; i < RUNS; i++) {
processRequest1()
}
console.timeEnd('processRequest1')
// ---------------------------------------------------------
// NO PRE-COMPILE REGEX
console.time('processRequest2')
function processRequest(){
let camId = /^camera-([0-9]+)/.exec('camera-1')
return camId
}
for (let i = 0; i < RUNS; i++) {
processRequest()
}
console.timeEnd('processRequest2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment