Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Array.find vs Array.indexOf (editted)
(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(v => v === TEST_NUM)
Array.includes
var TEST_NUM = 897495 var result = values.includes(TEST_NUM)
Array.indexOf
var TEST_NUM = 897495 var result = values.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):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark measures the performance of three methods to search for an element in an array: 1. `Array.includes` 2. `Array.find` 3. `Array.indexOf` The test case uses a large array (`values`) with 1,000,000 elements, and searches for a specific value (`TEST_NUM`). **Library and Special Features** None of the test cases use any external libraries or special JavaScript features that are not part of the standard language. **Options Compared** The three methods compared are: 1. `Array.includes` * Pros: Simple, straightforward implementation. It's often faster than other methods because it uses a simple linear search algorithm. * Cons: Not as efficient as other methods for large arrays, as it has to iterate over the entire array until it finds the desired element. 2. `Array.find` * Pros: More efficient than `Array.includes` and `Array.indexOf`, especially for large arrays. It's designed specifically for finding a single value in an array and returns it immediately if found. * Cons: May not be as fast as other methods if the desired element is not found, because it needs to iterate over the entire array until it finds the desired value. 3. `Array.indexOf` * Pros: Similar to `Array.includes`, but can be more efficient in some cases. It returns the index of the first occurrence of the specified value, which can be useful for certain use cases. * Cons: May not be as fast as `Array.find` for large arrays with many occurrences of the desired element. **Benchmark Results** The latest benchmark results show that: 1. `Array.includes` is the fastest method, executing approximately 577 executions per second on a Chrome 91 browser and Mac OS X 10.14.6 operating system. 2. `Array.indexOf` is slightly slower than `Array.includes`, executing around 82 executions per second. 3. `Array.find` is the slowest method, executing only about 57 executions per second. **Other Alternatives** If you need to search for an element in an array, other alternatives could include: 1. `forEach`: This method iterates over the array and executes a callback function for each element. While not specifically designed for finding an element, it can be used with some modifications. 2. Regular expressions: If you're dealing with arrays of strings or regular expression patterns, using regular expressions can be faster than traditional array search methods. 3. `Map` data structure: If you need to search for a value in an associative array (object), using a `Map` data structure can be more efficient. Keep in mind that the best approach depends on your specific use case and requirements.
Related benchmarks:
Array.includes() vs Array.indexOf()
IndexOf vs Includes array of numbers
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?