Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs findIndex vs for vs while vs indexed - 100 items
(version: 4)
Comparing performance of:
findIndex vs indexOf vs for vs while vs indexed
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var exampleItem1 = 'You will never find me!'; var exampleItem2 = 'I am invisible!'; var exampleArray = Array(100).fill('').map((x, i) => x + (i + 1)); var compare1 = x => x === exampleItem1; var compare2 = x => x === exampleItem2;
Tests:
findIndex
exampleArray.findIndex(compare1); exampleArray.findIndex(compare2);
indexOf
exampleArray.indexOf(exampleItem1); exampleArray.indexOf(exampleItem2);
for
let index1 = null; let index2 = null; for (let i = 0, l = exampleArray.length; i < l; i += 1) { if (index1 === null && compare1(exampleArray[i])) { index1 = i; continue; } if (index2 === null && compare2(exampleArray[i])) { index2 = i; continue; } if (index1 !== null && index2 !== null) break; }
while
let index1 = null; let index2 = null; let i = -1, l = exampleArray.length; while (++i < l) { if (index1 === null && compare1(exampleArray[i])) { index1 = i; continue; } if (index2 === null && compare2(exampleArray[i])) { index2 = i; continue; } if (index1 !== null && index2 !== null) break; }
indexed
const indexes = {}; for (let i = 0, l = exampleArray.length; i < l; i += 1) { indexes[exampleArray[i]] = i; } let index1 = indexes[exampleItem1]; let index2 = indexes[exampleItem2];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
findIndex
indexOf
for
while
indexed
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
899142.3 Ops/sec
indexOf
5753933.5 Ops/sec
for
2088736.1 Ops/sec
while
2143603.2 Ops/sec
indexed
1220225.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and its options. **Overview** The benchmark measures the performance of four different methods to find an element in an array: 1. `findIndex`: returns the index of the first occurrence of the specified value, or -1 if not found. 2. `indexOf`: returns the index of the first occurrence of the specified value, or -1 if not found. 3. `for` loop with indices: uses a traditional `for` loop to iterate over the array and find the index of the element. 4. `while` loop with indices: uses a `while` loop to iterate over the array and find the index of the element. 5. `indexed`: uses an object to store the indices of all elements in the array, then looks up the index of the specified value. **Options Compared** The options compared are: * `findIndex` vs `indexOf`: both return the index of the first occurrence of the specified value, but `findIndex` is generally considered faster and more efficient because it stops iterating as soon as it finds a match. * `for` loop with indices vs `while` loop with indices: both methods iterate over the array and find the index of the element, but `for` loops are often preferred because they are easier to read and maintain. However, `while` loops can be more flexible in certain situations. * `indexed`: uses an object to store indices, which allows for faster lookups compared to the other methods. **Pros and Cons** * `findIndex`: + Pros: generally faster, more efficient, and easier to use than `indexOf`. + Cons: may not work as expected in certain situations (e.g., when dealing with sparse arrays). * `indexOf`: + Pros: widely supported, easy to use. + Cons: slower and less efficient than `findIndex`, especially for large arrays. * `for` loop with indices: + Pros: easy to read and maintain, can be more flexible than `while` loops. + Cons: may not be as performant as other methods, especially for large arrays. * `while` loop with indices: + Pros: flexible, can be used in situations where a `for` loop would not work. + Cons: less readable and maintainable than `for` loops, can lead to performance issues if not implemented correctly. * `indexed`: + Pros: fastest lookup time, especially for large arrays. + Cons: requires more memory and computation resources than the other methods. **Other Considerations** * `findIndex` is generally preferred over `indexOf` because it is faster and more efficient. However, if you need to support older browsers that only support `indexOf`, you may need to use a fallback implementation. * The choice between `for` loops and `while` loops depends on the specific requirements of your project. If readability and maintainability are important, a `for` loop may be a better choice. However, if flexibility is more important, a `while` loop may be preferred. **Alternative Methods** Other methods that could be used to find an element in an array include: * Using a `Map` or `Set` data structure to store the elements and their indices. * Using a recursive function to search for the element. * Using a parallel processing algorithm (e.g., multi-threading or parallel computing) to speed up the search. However, these methods may not be as performant or efficient as the ones mentioned above, and may require more memory and computation resources.
Related benchmarks:
indexOf vs findIndex vs for vs while vs indexed - 500 items
indexOf vs findIndex vs for vs while vs indexed - 2000 items
findIndex vs indexOf for simple array 2
findIndex vs indexOf - JavaScript performance v2
Comments
Confirm delete:
Do you really want to delete benchmark?