Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ForEach then find index vs ForEach then includes vs ForEach then indexOf v2.2
(version: 3)
Comparing performance of:
ForEach then find index vs ForEach then include vs ForEach then indexOf
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var bigList = new Array(15000); bigList.fill(0); bigList = bigList.map((el, idx) => el = idx); var idList = new Array(15000); idList.fill(0); idList = idList.map((el) => el = Math.floor(Math.random() * 15000))
Tests:
ForEach then find index
idList.forEach((itemId) => { bigList.findIndex((bigListItem) => bigListItem === itemId); });
ForEach then include
bigList.forEach((bigListItem, index) => { idList.includes(bigListItem) });
ForEach then indexOf
bigList.forEach((bigListItem, index) => { idList.indexOf(bigListItem) !== -1 });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
ForEach then find index
ForEach then include
ForEach then indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ForEach then find index
16.0 Ops/sec
ForEach then include
10.9 Ops/sec
ForEach then indexOf
58.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, along with the pros and cons of each approach. **Benchmark Overview** The test measures the performance of three different approaches to iterate over an array and find an element using `includes` or `indexOf`. The array size is 15,000 elements, which is a typical size for such benchmarks. The goal is to determine which approach is the fastest. **Approaches Being Tested** 1. **ForEach then FindIndex**: This approach uses the `forEach` method to iterate over the `bigList` and call `findIndex` on it for each element in the `idList`. 2. **ForEach then Includes**: Similar to the previous one, but instead of using `findIndex`, this approach uses the `includes` method. 3. **ForEach then indexOf v2.2**: This is a variant of the previous approach, which seems to be an older version of the `indexOf` method. **Problems and Trade-Offs** Here are some considerations for each approach: * **Performance**: The order in which the loops are executed can significantly impact performance. In this case, `forEach then FindIndex` is likely faster than `ForEach then includes` because finding an element using `findIndex` only checks one index, while checking if an element is present using `includes` scans the array from scratch. * **Memory Usage**: The use of `forEach` and loops can be memory-intensive. In this case, all three approaches create a new scope for each iteration, which means they consume more memory than alternative methods that reuse existing scopes or buffers. * **Browser Support**: Some older browsers may not support certain methods like `includes` or `indexOf v2.2`. **Library Used:** None. **Special JavaScript Features or Syntax:** The benchmark uses the following features: * `forEach`: a built-in array method that executes a callback function for each element in an array. * `findIndex`, `includes`, and `indexOf` are built-in methods for searching arrays. * The v2.2 version of `indexOf` may be specific to older browsers or environments. **Other Alternatives** Some alternative approaches that could be tested include: * Using a more efficient data structure like a Set, which would eliminate the need for finding an element using `findIndex`. * Reusing existing loops and buffers instead of creating new scopes for each iteration. * Using WebAssembly or other low-level optimizations to improve performance. Note that these alternatives may require significant changes to the benchmark code and are not necessarily relevant to the original intent of measuring the performance difference between three specific approaches.
Related benchmarks:
findIndex primitive vs findIndex object
Object arrays: findIndex vs forEach
Lodash sort vs array.prototype.sort - compare with taking ids from different array
dealing with array of array which array should come first?
JS findIndex vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?