Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object arrays: findIndex vs for loop 2
(version: 0)
Testing finding an object array via findIndex or by using a for loop
Comparing performance of:
findIndex vs for loop
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(1000000); arr.fill({ id: 0 }); arr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
arr.findIndex((itm) => itm.id === foo);
for loop
for (let i = 0; i < arr.length; i++) { if (arr[i].id === foo) break; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
for loop
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 JSON and explain what's being tested. **Benchmark Overview** The benchmark is comparing two approaches to find an element in a large array: using `Array.prototype.findIndex()` (also known as "findIndex") or a traditional for loop. The goal is to determine which approach is faster. **Options Compared** Two options are compared: 1. **`arr.findIndex((itm) => itm.id === foo);`**: This uses the `findIndex()` method, which returns the index of the first element in the array that satisfies the provided condition (in this case, `foo`). If no element is found, -1 is returned. 2. **`for (let i = 0; i < arr.length; i++) {\r\n if (arr[i].id === foo) break;\r\n}`**: This uses a traditional for loop to iterate through the array and find the first element that matches `foo`. **Pros and Cons** * **FindIndex()**: + Pros: concise, easy to read, and maintainable. + Cons: may incur additional overhead due to the need to create a callback function. * **For Loop**: + Pros: well-known, widely supported, and can be optimized with caching or other techniques. + Cons: verbose, harder to read and maintain, especially for large arrays. **Library Used** In this benchmark, `Array.prototype.findIndex()` is used. The `findIndex()` method is a part of the ECMAScript standard and is implemented by most modern browsers, including Chrome 106. **Special JS Feature or Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. **Other Considerations** * Performance: Finding an element in a large array can be a performance-critical operation. The results will likely depend on the size of the array and the specific hardware used. * Browser Support: While `findIndex()` is widely supported, older browsers may not have it available or may behave differently. **Alternatives** Other approaches to finding an element in a large array might include: 1. **Binary Search**: A more complex approach that can be faster for large arrays, but requires additional overhead. 2. **Sparse Arrays**: A technique where only non-zero elements are stored in the array, which can reduce memory usage and potentially improve performance. 3. **Caching**: Precomputing the results of previous searches to avoid repeated work. Keep in mind that these alternatives might not be as straightforward or easy to implement as `findIndex()` or a for loop.
Related benchmarks:
Object arrays: findIndex vs for loop2
Object arrays: findIndex vs for loop (length cached)
Object arrays: findIndex vs for loop vs some
Object arrays: findIndex vs for loop (Small amount of entries)
findIndex vs for loop with plain number array
Comments
Confirm delete:
Do you really want to delete benchmark?