Skip to content

Instantly share code, notes, and snippets.

@clintonyeb
Created November 1, 2018 23:33
Show Gist options
  • Save clintonyeb/b9b84ac66447a7d9c9b3ff3655db4695 to your computer and use it in GitHub Desktop.
Save clintonyeb/b9b84ac66447a7d9c9b3ff3655db4695 to your computer and use it in GitHub Desktop.
Benchmark Testing of JavaScript indexOf and StartsWith when both can work
function performanceTest(testFunction, iterations) {
const start = process.hrtime.bigint();
for (let i = 0; i < iterations; i++) {
testFunction();
}
const end = process.hrtime.bigint();
const time = end - start;
console.log(`Benchmark took ${time} nanoseconds`);
return time;
}
const string = "0xf85d8512f9a6fc3c7fcd1690980b7ca6e12884c1b81aafd0f4ff342e5cbe2110534535353534535353535443"
function testStartsWith() {
string.startsWith("0x")
}
function testIndexOf() {
string.indexOf("0x") == 0
}
const iterations = 10000
console.log("Testing startsWith:::")
performanceTest(testStartsWith, iterations)
console.log("Completed startsWith.\n\n")
console.log("Testing indexOf:::")
performanceTest(testIndexOf, iterations)
console.log("Completed indexOf.\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment