Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Array IndexOf vs includes vs findIndex vs find1
(version: 0)
Comparing performance of:
includes vs find
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var rr = [{id: 10000}] var i = 0; while (i <= 10000) arr[i] = { id: i++ };
Tests:
includes
const included = arr.filter(el => rr.includes(el));
find
const item = arr.filter(el => rr.find(item => item.id === el.id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
find
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 benchmark JSON and explain what is being tested, compared, and some pros/cons of each approach. **Benchmark Definition** The benchmark definition represents a JavaScript microbenchmark that tests various ways to find an element in an array. The script preparation code creates a large array `arr` with 10,000 elements, and another array `rr` with 1 element for comparison. The test cases use these arrays to filter out the matching elements. **Options Compared** The benchmark compares four different approaches: 1. **includes**: Uses the `includes()` method to check if an element is in the array. 2. **find**: Uses the `find()` method to find the first occurrence of a function that returns a boolean value indicating whether the element is in the array. 3. **indexOf**: Although not explicitly mentioned, it's implied as an alternative to includes. However, it's worth noting that `indexOf()` is similar to `includes()`, but with some subtle differences. **Pros/Cons** Here's a brief overview of each approach: 1. **includes**: * Pros: Simple and efficient, widely supported by browsers. * Cons: Can be slower than other methods for very large arrays due to the way it searches the array from head to tail. 2. **find**: * Pros: More flexible than `includes()`, as it can return early when a match is found, making it more efficient for large arrays. * Cons: Can be slower than `includes()` for small arrays due to the overhead of searching the array. 3. **indexOf** (implied): * Pros: Similar performance characteristics to `includes()`. * Cons: Not explicitly supported by all browsers, and its behavior can vary depending on the browser. **Library and Special Features** The benchmark uses no external libraries, but it does rely on the built-in `Array.prototype.includes()` method introduced in ECMAScript 2015 (ES6). This method is widely supported by modern browsers and provides a simple way to check if an element is in an array. No special JavaScript features or syntax are used in this benchmark. **Other Alternatives** For larger arrays, more efficient algorithms like binary search can be used. However, these require modifying the underlying data structure (e.g., sorting the array) which may not be feasible for every use case. Another alternative is to use a library like Lodash, which provides optimized versions of these methods. However, this would add additional dependencies and complexity to the benchmark. In summary, this benchmark compares simple yet efficient approaches to find an element in an array, highlighting the trade-offs between performance, simplicity, and flexibility.
Related benchmarks:
indexOf vs findIndex with a simple case
findIndex vs indexOf for simple array 2
Array find with indexOf vs includes
JS Array IndexOf vs includes vs findIndex vs find 2
Comments
Confirm delete:
Do you really want to delete benchmark?