Created
November 1, 2018 23:33
-
-
Save clintonyeb/b9b84ac66447a7d9c9b3ff3655db4695 to your computer and use it in GitHub Desktop.
Benchmark Testing of JavaScript indexOf and StartsWith when both can work
This file contains hidden or 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 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