Last active
August 22, 2017 07:59
-
-
Save robpalme/4f405ba67f39ae6f536f83598100bcfa to your computer and use it in GitHub Desktop.
Linear string search perf
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() { | |
const Y_IX = 1e6; | |
const str = "x".repeat(Y_IX) + "y"; | |
function regexp (str) { | |
return /y/.exec(str).index; | |
} | |
function indexOf(str) { | |
return str.indexOf("y"); | |
} | |
function noop() { return Y_IX; } | |
// MEASUREMENT INFRASTRUCTURE | |
const applyFunctions = [ | |
regexp, | |
indexOf, | |
noop, | |
]; | |
const ITERATIONS = 1e5; | |
function test(applyFun) { | |
for (var i = 0; i < ITERATIONS; ++i) { | |
if (applyFun(str) !== Y_IX) { throw 1 }; | |
} | |
} | |
// Warmup | |
for (const applyFun of applyFunctions) { | |
console.time("First " + applyFun.name); | |
applyFun(str); | |
console.timeEnd("First " + applyFun.name); | |
test(applyFun); | |
} | |
// Measure | |
for (const applyFun of applyFunctions) { | |
console.time("Optimized " + applyFun.name); | |
test(applyFun); | |
console.timeEnd("Optimized " + applyFun.name); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment