Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs findIndex vs for vs while vs indexed - 2000 items
(version: 0)
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(2000).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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance of four different approaches to find an item in an array: `indexOf`, `findIndex`, `for` loop, and `while` loop. The array contains 2000 items, each with a unique value. **Options Compared** 1. **`indexOf`**: This method returns the index of the first occurrence of the specified element. If not found, it returns -1. 2. **`findIndex`**: This method returns the index of the first occurrence of the specified element. If not found, it returns -1. 3. **`for` loop**: This approach uses a traditional `for` loop to iterate through the array and find the desired item. 4. **`while` loop**: This approach uses a `while` loop to iterate through the array and find the desired item. **Pros and Cons of Each Approach** 1. **`indexOf`**: * Pros: Simple, widely supported, and well-documented. * Cons: Returns -1 for non-existent elements, which may not be desirable in all cases. 2. **`findIndex`**: * Pros: Similar to `indexOf`, but returns -1 instead of a boolean value. * Cons: Less intuitive than `indexOf`. 3. **`for` loop**: * Pros: Allows for manual control over iteration, can be optimized for performance. * Cons: More verbose and less readable than other approaches. 4. **`while` loop**: * Pros: Similar to the `for` loop, but with more flexibility in terms of indexing. * Cons: May require additional bookkeeping for index management. **Library Used** None explicitly mentioned in the benchmark definition or test cases. **Special JavaScript Features/Syntax** No special features or syntax are used in this benchmark. **Other Alternatives** Other approaches to find an item in an array include: 1. **`Array.prototype.findIndex()`**: Similar to `findIndex`, but returns -1 instead of a boolean value. 2. **Using `Map` data structure**: Convert the array to a `Map` and use the `get()` method to retrieve the value. 3. **Using `SparseArray`**: Use a sparse array to store the elements and then search for the desired item. In conclusion, this benchmark provides a comprehensive comparison of four different approaches to find an item in an array, highlighting their strengths and weaknesses. The results can help developers make informed decisions about which approach to use in specific scenarios.
Related benchmarks:
indexOf vs findIndex with a simple case
indexOf vs findIndex vs for vs while vs indexed - 100 items
indexOf vs findIndex vs for vs while vs indexed - 500 items
findIndex vs indexOf - JavaScript performance v2
Comments
Confirm delete:
Do you really want to delete benchmark?