console.clear()
let a = []
// populating array data
for (let i = 0; i < 100000; i++) {
a.push(i)
}
let testNum = 90000
let found
let totalMS4ForLoop = 0
let totalMS4ForIn = 0
let totalMS4IndexOf = 0
let start
let end
// simulating 10000 requests which are come consecutively
for (o = 0; o < 10000; o++) {
start = Date.now()
for (let i = 0; i < a.length; i++) {
if (a[i] == testNum) { found = a[i]; break }
}
end = Date.now()
totalMS4ForLoop += end - start
start = Date.now()
for (let j in a) {
if (a[j] == testNum) { found = a[j]; break }
}
end = Date.now()
totalMS4ForIn += end - start
start = Date.now()
found = a[a.indexOf(testNum)]
end = Date.now()
totalMS4IndexOf += end - start
}
console.log("100000x10000 for-loop executions took total " + totalMS4ForLoop + " ms.")
console.log("100000x10000 for-in executions took total " + totalMS4ForIn + " ms.")
console.log("100000x10000 indexOf executions took total " + totalMS4IndexOf + " ms.")
100000x10000 for-loop executions took total 283 ms.
100000x10000 for-in executions took total 33368 ms.
100000x10000 indexOf executions took total 113 ms.