Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs forEach
(version: 0)
compare performance of findIndex and forEach in removing the first result in an array
Comparing performance of:
findIndex vs forEach
Created:
3 years ago
by:
Registered User
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); arr = arr.filter((value, index, self) => self.indexOf(value) === index)
Tests:
findIndex
const index = arr.findIndex((itm) => itm.id === foo); arr.splice(index,1);
forEach
arr.forEach((value, index) => { if (value.id === foo) { arr.splice(index, 1); } });
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
394848.8 Ops/sec
forEach
101950408.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. **Benchmark Description** The benchmark compares the performance of two JavaScript methods: `Array.prototype.findIndex()` and `Array.prototype.forEach()`. The goal is to find the index of the first element that satisfies a certain condition and then remove that element from the array. **Options Compared** 1. **FindIndex**: Uses the `findIndex()` method, which returns the index of the first element in the array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. 2. **ForEach**: Uses the `forEach()` method, which executes a provided function for each element in the array. In this case, we use it to find the first element that satisfies the condition and then remove it from the array. **Pros and Cons** * **FindIndex**: + Pros: Generally faster than `forEach` because it uses an optimized algorithm that stops searching as soon as it finds a match. + Cons: Returns -1 if no elements satisfy the testing function, which might require additional checks in your code. * **ForEach**: + Pros: More flexible and can be used for more complex operations than just finding and removing a single element. + Cons: Generally slower than `findIndex` because it needs to iterate over all elements in the array. **Other Considerations** * **Performance**: As mentioned earlier, `findIndex` is generally faster than `forEach`. However, the actual performance difference depends on the specific use case and the size of the input array. * **Readability and Maintainability**: Choose the method that best fits your code's logic and readability requirements. **Library/Functionality Used** In this benchmark, no external libraries are used. The JavaScript methods `Array.prototype.findIndex()` and `Array.prototype.forEach()` are part of the standard library. **Special JS Features/Syntax** There is no special JavaScript feature or syntax being tested in this benchmark. Both `findIndex` and `forEach` are standard methods that can be used with any array. **Alternatives** Other alternatives for finding the index of a single element in an array could include: * Using the `indexOf()` method, which returns the index of the first occurrence of a specified value. * Using the `search()` method, which searches the array from the start and returns the index of the first match. * Implementing a custom search algorithm using loops or other optimization techniques. However, these alternatives are not being tested in this specific benchmark.
Related benchmarks:
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf vs includes - JavaScript performance
JS findIndex vs forEach
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?