Loop array with idx

Benchmark created on


Setup

const results = [];
function capture(value,idx) {
  if (results.push(value) === 10 && idx % 2)
  results.length = 0;
}
const len = 20
const arr = Array.from({ length: len })


for(let i = 0;i<len;i++){
	arr[i] = Math.random()
}

Test runner

Ready to run.

Testing in
TestOps/sec
arr.entries
for (const [index, val] of arr.entries()){
	capture(val)
}
ready
while loop, not inverted
let i = 0
let j = arr.length
while (i<j) {
	capture(arr[i])
	i++
} 
ready
while i--
let i = arr.length
while (i--) capture(arr[i])
ready
forEach with index
arr.forEach((val,i) => capture(val))
ready
simple for const loop, manual i++ indexing
let i = 0
for(const val of arr){
	i++
	capture(val)
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.