Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Array.find vs Array.indexOf (with object)
(version: 0)
Comparing performance of:
Array.find vs Array.includes vs Array.indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var values = [] for (var i = 0; i < 1000000; i++) { values.push({i}) }
Tests:
Array.find
var TEST_NUM = 897495 var result = values.find(({i}) => i === TEST_NUM)
Array.includes
var TEST_NUM = 897495 var mappedValues = values.map(({i}) => i) var result = mappedValues.includes(TEST_NUM)
Array.indexOf
var TEST_NUM = 897495 var mappedValues = values.map(({i}) => i) var result = mappedValues.indexOf(TEST_NUM) !== -1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.find
Array.includes
Array.indexOf
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):
**Overview** The provided JSON represents a JavaScript benchmark test on MeasureThat.net. The test compares the performance of three different approaches to find an element in an array: `Array.find()`, `Array.includes()`, and `Array.indexOf()`. **Benchmark Definition** The benchmark definition specifies the following: * A large array of 1,000,000 objects with a single property `i`. * Three test cases: + `Array.find()`: Find the first element in the array that matches the condition `i === TEST_NUM`, where `TEST_NUM` is a constant value (897495). + `Array.includes()`: Check if the array contains the element with the index `TEST_NUM`. + `Array.indexOf()`: Get the index of the element with the index `TEST_NUM` in the array. **Options Compared** The benchmark compares three different approaches to find an element in the array: * **`Array.find()`**: This method returns the first element that satisfies the provided testing function. It is a more concise and expressive way to find an element in the array. * **`Array.includes()`**: This method checks if the array contains an element with the specified value (or index, in this case). It returns `true` if the array contains the element, and `false` otherwise. * **`Array.indexOf()`**: This method returns the index of the first occurrence of the specified value. If the value is not found, it returns `-1`. In this benchmark, we use a negation (`!== -1`) to check for presence. **Pros and Cons** Here are some pros and cons of each approach: * **`Array.find()`**: Pros: + Concise and expressive + Returns the first matching element Cons: + May return `undefined` if no matching element is found * **`Array.includes()`**: Pros: + Simple and easy to read + Returns a boolean value (`true` or `false`) Cons: + May be slower than `Array.find()` + Does not provide the index of the element * **`Array.indexOf()`**: Pros: + Fast and efficient ( returns the index directly) Cons: + Returns `-1` if the element is not found, which can lead to confusion **Library: None** There are no libraries involved in this benchmark. **Special JS Feature or Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Alternative Approaches** Other approaches to find an element in an array could include: * Using `Array.prototype.some()` and a callback function * Using `Array.prototype.reduce()` with an initial value of `0` * Using a simple loop to iterate through the array However, these approaches are not included in this benchmark. Note that the performance differences between these methods can be significant, especially for large arrays. Therefore, it's essential to choose the right approach depending on your specific use case and performance requirements.
Related benchmarks:
IndexOf vs Includes array of numbers
IndexOf vs Includes vs find
Array find with indexOf vs includes
array indexOf vs includes vs some v3
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?