Last active
July 15, 2022 07:39
-
-
Save danleyb2/b48f8b9a479f8d617961ef5e2641d8ac to your computer and use it in GitHub Desktop.
Regex Pre-Compilation Performance difference
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
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