Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Object arrays: map + indexOf vs for loop vs findIndex for finding and editing an array item.
Test finding an object array via findIndex or by using a for loop, and edit the value of that index.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser:
Firefox 129
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
findIndex
59375.5 Ops/sec
for loop
76016.3 Ops/sec
Map + IndexOf
11090.9 Ops/sec
Script Preparation code:
/* make a new array with 15k items */ var arr = new Array(15000); /* fill array tems with basic object */ arr.fill({ id: 0 }); /* generate 2 random numbers in our 15k range */ var foo = Math.floor(Math.random() * 15000); var bar = Math.floor(Math.random() * 15000); /* set the index of arr[foo] to foo for our tests */ arr[foo].id = foo;
Tests:
findIndex
const bookFreePlatform = (arr, foo, bar) => { /* find array index where id == foo */ var index = arr.findIndex(item => item.id === foo); /* findIndex returns -1 on fail, so test for that */ if ( index != -1 ) { /* ok, we have an index, lets edit that entry, and console log it for fun */ arr[index].id = bar; console.log(arr[index]); } /* return altered array */ return arr; } bookFreePlatform(arr, foo, bar);
for loop
const bookFreePlatform = (arr, foo, bar) => { /* find array item where id == foo */ for (let i = 0; i < arr.length; i++) { /* not an error check, just a simple comparison */ if (arr[i].id === foo) { /* if we got here, it found and index, lets edit it and log it again */ arr[i].id = bar; console.log(arr[i]); /* stop the loop to save time and resources, we're done */ break; } } /* return altered array */ return arr; } bookFreePlatform(arr, foo, bar);
Map + IndexOf
const bookFreePlatform = (arr, foo, bar) => { /* find array index where id == foo */ var index = arr.map(item => item.id).indexOf(foo); /* findIndex returns -1 on fail, so test for that */ if ( index != -1 ) { /* ok, we have an index, lets edit that entry, and console log it for fun */ arr[index].id = bar; console.log(arr[index]); } /* return altered array */ return arr; } bookFreePlatform(arr, foo, bar);