Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs findIndex (Array prototype methods)
(version: 0)
Measuring which is faster
Comparing performance of:
Array.prototype.find vs Array.prototype.findIndex
Created:
7 years ago
by:
Registered User
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);
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:
Run details:
(Test run date:
16 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.find
1570.7 Ops/sec
Array.prototype.findIndex
1591.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explore what's being tested. **What is tested?** The provided JSON represents two test cases: `Array.prototype.find` and `Array.prototype.findIndex`. Both tests are comparing the performance of these two methods on an array prototype, specifically when searching for a value equal to 1E5 (100,000). **Options compared** In this benchmark, only two approaches are being compared: 1. **Array.prototype.find**: This method returns the first element in the array that satisfies the provided condition (in this case, `item == 1E5`). If no such element exists, it returns undefined. 2. **Array.prototype.findIndex**: This method returns the index of the first element in the array that satisfies the provided condition (in this case, `item == 1E5`). If no such element exists, it returns -1. **Pros and Cons of each approach** Here's a brief summary: * **Array.prototype.find**: + Pros: More concise and expressive, as it directly returns the desired value. + Cons: May be slower due to the overhead of searching for the first matching element. * **Array.prototype.findIndex**: + Pros: Faster, as it only needs to find the index of the first matching element, rather than the element itself. + Cons: Returns an index, which might not be as intuitive or convenient in some cases. **Library and purpose** Neither `find` nor `findIndex` relies on any external libraries. They are both built-in methods of the JavaScript Array prototype. **Special JS feature or syntax** None mentioned explicitly, but keep in mind that modern JavaScript engines often optimize these methods for performance. **Other alternatives** If you wanted to test alternative approaches, you might consider: * Using a custom loop or function to achieve the same result (e.g., `arr.forEach` or a simple iteration). * Utilizing other array methods like `some`, `every`, or `includes`. * Implementing a custom binary search algorithm for finding an element in the array. However, these alternatives would not be directly comparable to the built-in `find` and `findIndex` methods, as they would likely have different performance characteristics and use cases.
Related benchmarks:
find vs findIndex (Array prototype methods) 22
find vs findIndex (Array prototype methods) stop at first found
find vs indexOf (Array prototype methods)
find vs findIndex (Array prototype methods) - using objects
Comments
Confirm delete:
Do you really want to delete benchmark?