Skip to content

Instantly share code, notes, and snippets.

@robpalme
Last active August 22, 2017 07:59
Show Gist options
  • Save robpalme/4f405ba67f39ae6f536f83598100bcfa to your computer and use it in GitHub Desktop.
Save robpalme/4f405ba67f39ae6f536f83598100bcfa to your computer and use it in GitHub Desktop.
Linear string search perf
(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