Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs forEach RSE
(version: 0)
Comparing performance of:
findIndex vs ForEach
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); 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);
ForEach
arr.forEach((value) => { if (value.id === foo) { return; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
ForEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
56689.6 Ops/sec
ForEach
148997.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition:** The benchmark is comparing two approaches to find an element in an array: 1. `arr.findIndex((itm) => itm.id === foo);` 2. `arr.forEach((value) => {\r\n if (value.id === foo) {\r\n return;\r\n }\r\n});` **What's being tested:** The benchmark is measuring the performance difference between using `findIndex` and a manual loop (`forEach`) to find an element in an array. **Options compared:** Two options are being compared: 1. **`findIndex`**: A built-in Array method that returns the index of the first element that satisfies the provided condition. 2. **Manual loop (`forEach`)**: A traditional approach using a `forEach` loop to iterate through the array and return as soon as the desired element is found. **Pros and Cons of each approach:** 1. **`findIndex`**: * Pros: + Efficient, as it only iterates through the array once. + Returns the index directly, making it easier to handle results. * Cons: + May not work as expected if the array is empty or contains no elements that match the condition. 2. **Manual loop (`forEach`)**: * Pros: + Works for any array size and content. + Can be more intuitive for developers who are familiar with loops. * Cons: + Less efficient than `findIndex`, as it requires iterating through the entire array. **Library/Language features:** There are no specific libraries or language features being tested in this benchmark. However, it's worth noting that the use of the `forEach` loop is a fundamental concept in JavaScript and other languages with similar syntax. **Special JS feature/syntax:** None mentioned in the provided JSON. **Other considerations:** When deciding between these two approaches, consider the following: * Use `findIndex` when: + You need to find an element that satisfies a specific condition. + Performance is critical, and you want to avoid unnecessary iterations through the array. * Use a manual loop (`forEach`) when: + You're working with arrays of varying sizes or content. + You prefer a more traditional approach to iterating through arrays. **Alternatives:** Other alternatives for finding an element in an array include: 1. `indexOf()`: Similar to `findIndex`, but returns -1 if no element is found. 2. `includes()`: A newer method that checks if an element exists within the array without returning its index. 3. Using a third-party library like Lodash's `some()` or `find()` methods. Keep in mind that these alternatives may have their own pros and cons, depending on your specific use case and requirements.
Related benchmarks:
findIndex vs indexOf - JavaScript performance
findIndex vs forEach until final
JS findIndex vs forEach
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?