Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs findIndex (Array prototype methods) vs includes
(version: 0)
Measuring which is faster
Comparing performance of:
Array.prototype.find vs Array.prototype.findIndex vs includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
Array.prototype.find
const item = arr.find(item => item == 1E5);
Array.prototype.findIndex
const index = arr.findIndex(item => item == 1E5);
includes
const included = arr.includes(1E5)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.find
Array.prototype.findIndex
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:122.0) Gecko/20100101 Firefox/122.0
Browser/OS:
Firefox 122 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.find
3292.6 Ops/sec
Array.prototype.findIndex
3374.0 Ops/sec
includes
19339.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the Benchmark Definition and the individual test cases. **Benchmark Definition** The provided JSON defines a benchmark for measuring the performance of three Array prototype methods: `find`, `findIndex`, and `includes`. The script preparation code creates an array with 100,000 elements, each initialized to its index. This setup is used to search for specific values within the array. In essence, this benchmark tests which method is faster when searching for a single value in a large array. **Options Compared** The three methods being compared are: 1. `find`: Returns the first element in the array that satisfies the provided testing function. 2. `findIndex`: Returns the index of the first element in the array that satisfies the provided testing function. 3. `includes`: Returns true if an element with the specified value exists in the array. **Pros and Cons** * **`find`**: * Pros: Simple, intuitive, and widely supported. It's a good choice when you're searching for an element that meets a specific condition. * Cons: Can be slow for large arrays because it needs to iterate over each element until it finds the match or reaches the end of the array. * **`findIndex`**: * Pros: Similar to `find`, but returns the index of the first matching element. It can be faster than `find` for large arrays since it doesn't need to return the value itself. * Cons: Returns an index, which might not be as useful as having the actual value returned by `find`. * **`includes`**: * Pros: Fastest of the three methods. It uses a binary search approach to find the element in O(log n) time complexity. * Cons: Only returns true or false, which might not be enough for some use cases. **Library and Purpose** The `Array.prototype.find`, `Array.prototype.findIndex`, and `includes` methods are built-in methods provided by JavaScript's Array prototype. They're designed to make it easy to work with arrays in a more functional programming style. * `find`: Returns the first element that satisfies the provided testing function. * `findIndex`: Returns the index of the first element that satisfies the provided testing function. * `includes`: Returns true if an element with the specified value exists in the array. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax being tested here. The benchmark focuses on comparing the performance of built-in Array methods. **Other Alternatives** If you need to search for elements in an array, some alternative approaches include: * Using a `for` loop to iterate over the array. * Utilizing libraries like Lodash or Ramda, which provide more functional programming-style functions for working with arrays. * Converting the array to a Map (if the elements are unique) and using the `has` method to check for existence. Keep in mind that these alternatives might have different performance characteristics compared to the built-in Array methods being tested here.
Related benchmarks:
find vs findIndex (Array prototype methods)
find vs findIndex (Array prototype methods) with console.log
find vs findIndex (Array prototype methods) 22
find vs findIndex (Array prototype methods) stop at first found
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?