Created
November 26, 2024 06:28
-
-
Save ankurparihar/6b4551bd72bca7cd173d17c0d1ba849f to your computer and use it in GitHub Desktop.
Array indexOf performance
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 findString(length=1) { | |
// const num1 = String(Math.floor(Math.random() * Math.pow(10, length))); | |
const string1 = new Array(length).fill(Math.floor(Math.random() * 10)).join(""); | |
const string2 = new Array(length).fill("a").join(""); | |
const string3 = new Array(length).fill("a").join(""); | |
console.log(string1 , string2, string3) | |
// create 100000 strings, with only the last string being the matching one | |
const strings = new Array(999999).fill(string1); | |
strings.push(string2); | |
// start and measure the process | |
const start = performance.now(); | |
// console.time(); | |
const i = strings.indexOf(string3); | |
// console.timeEnd(); | |
const end = performance.now(); | |
console.log(i, end-start); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment