Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs includes in filter
(version: 0)
Comparing performance of: findIndex vs includes in filter
Comparing performance of:
findIndex vs includes
Created:
4 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 arr2 = []; while(arr2.length < 5000){ var r = Math.floor(Math.random() * 10000) + 1; if(arr2.findIndex(i=>i.id===r) === -1) arr2.push({id:r}); } var foo =1;
Tests:
findIndex
arr2.filter(d=>arr.findIndex(item=>item.id===d.id)===-1);
includes
var idList=arr.map(i=>i.id) arr2.filter(d=>!idList.includes(d.id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
includes
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark compares the performance of two approaches: 1. `findIndex`: * Uses the `findIndex` method, which returns the index of the first element in the array that satisfies the provided testing function. 2. `includes`: * Uses a custom implementation with `map` and `includes`, where the `map` function generates an array of IDs from the original array, and then uses `includes` to check if each ID is present in the filtered array. **Options Compared** The benchmark compares two options: 1. **findIndex**: This method returns the index of the first element that satisfies the testing function. It's a built-in JavaScript method that's optimized for performance. 2. **Custom implementation with `map` and `includes`**: This approach generates an array of IDs from the original array using `map`, and then uses `includes` to check if each ID is present in the filtered array. **Pros and Cons** Here are some pros and cons of each approach: 1. **findIndex**: * Pros: Optimized for performance, easy to use, and built-in. * Cons: May not be as flexible or customizable as other approaches. 2. **Custom implementation with `map` and `includes`**: * Pros: Can be more flexible and customizable, can be used in situations where `findIndex` is not available or supported. * Cons: Requires extra memory allocation for the temporary array of IDs, may have performance overhead due to the use of `map` and `includes`. **Library and Purpose** In this benchmark, no external libraries are used. **Special JS Feature/Syntax** The benchmark uses a modern JavaScript feature: 1. **Template literals**: The code uses template literals (e.g., `"var arr = new Array(15000);"`). Template literals are a feature introduced in ECMAScript 2015 that allows for more concise and readable string interpolation. **Other Alternatives** Other alternatives to `findIndex` could include: 1. **indexOf** (for older browsers or environments that don't support `findIndex`): This method returns the index of the first element that satisfies the testing function, similar to `findIndex`. 2. **for...of loop**: A manual implementation using a `for...of` loop to iterate over the array and check each element. However, these alternatives are not used in this benchmark, which focuses on comparing the performance of `findIndex` with a custom implementation using `map` and `includes`.
Related benchmarks:
FindIndex + splice vs filter FindIndex
findIndex vs includes
findIndex vs indexOf vs find vs filter - JavaScript performance
test findIndex vs includes vs map & indexOf
findIndex vs indexOf vs includes - JavaScript performance
Comments
Confirm delete:
Do you really want to delete benchmark?