Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs includs
(version: 0)
Measuring which is faster
Comparing performance of:
Array.prototype.find vs Array.prototype.findIndex
Created:
5 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.includes(1E5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.find
Array.prototype.findIndex
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 explanation of the benchmark. **Benchmark Purpose** The goal of this benchmark is to compare the performance of two methods in the `Array` prototype: `find()` and `includes()`. The methods are being tested on an array generated by the script preparation code, which creates a large array with numbers from 0 to 100,000. **Methods Being Compared** 1. **Array.prototype.find()**: This method returns the value of the first element in the array that satisfies the provided testing function. 2. **Array.prototype.includes()**: This method checks if a specified element is present in the array. **Approaches and Pros/Cons** Both methods are generally fast, but they have some differences: * `find()` method has to iterate through the array until it finds the first matching element, which can lead to slower performance for larger arrays. * `includes()` method returns a boolean result immediately if the specified value is found in the array, making it potentially faster. **Pros and Cons** * **Find()**: Pros - Can be useful when you want to get the actual matching element rather than just checking its presence. Cons - Slower performance for larger arrays due to the need to iterate through elements. * **Includes():** Pros - Faster performance for large arrays as it returns a boolean result immediately. Cons - Returns a boolean value instead of the actual matching element. **Library Usage** Neither `find()` nor `includes()` methods use any external libraries. **Special JavaScript Features/Syntax** There is no special JavaScript feature or syntax being tested in this benchmark. **Other Alternatives** If you're looking for alternative methods, here are some: * For finding the first occurrence of an element: You can also use a `for...of` loop with `break` statement to exit the loop once the condition is met. * For checking if an element is present in an array without using the `includes()` method: You can create your own implementation or use a library like Lodash. Example of alternative implementation for finding the first occurrence: ```javascript function findFirst(arr, value) { for (let i = 0; i < arr.length; i++) { if (arr[i] === value) return arr[i]; } return undefined; } ``` Keep in mind that while this approach is straightforward, it may not be as efficient as the `find()` method.
Related benchmarks:
find vs findIndex (Array prototype methods)
find vs findIndex (Array prototype methods) 22
find vs findindex with condition test
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?