Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs findIndex (Array prototype methods) - using objects
(version: 0)
Measuring which is faster when elements are objects
Comparing performance of:
Array.prototype.find vs Array.prototype.findIndex
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = { id: i++, description: "something" };
Tests:
Array.prototype.find
const item = arr.find(item => item.id == 1E5);
Array.prototype.findIndex
const index = arr.findIndex(item => item.id == 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:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.find
1518.0 Ops/sec
Array.prototype.findIndex
2083.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a benchmark test on the MeasureThat.net website, designed to measure the performance difference between `find` and `findIndex` methods in JavaScript's Array prototype when searching for objects within an array. **Test Case Description** The test case consists of two sub-tests: 1. **Array.prototype.find**: This method is used to find the first element in the array that satisfies a given condition. 2. **Array.prototype.findIndex**: This method returns the index of the first element in the array that satisfies a given condition. In this specific benchmark, both methods are tested on an array of objects, where each object has an `id` property that is used to compare against the value passed as an argument to the callback function. **Options Compared** The two options being compared are: 1. **Array.prototype.find**: This method returns the first matching element or undefined if no match is found. 2. **Array.prototype.findIndex**: This method returns the index of the first matching element or -1 if no match is found. **Pros and Cons** * **Array.prototype.find**: * Pros: More readable code, as it uses a more conventional `find` operation with a callback function. * Cons: May be slower than `findIndex` due to the overhead of searching for an object in the array using the value of the property passed as an argument. * **Array.prototype.findIndex**: * Pros: Can provide faster performance, especially when dealing with large arrays, since it only searches up to the index where the condition is false. * Cons: May require more understanding of indices and array manipulation, as well as more verbose code. **Library Usage** There are no libraries explicitly mentioned in this benchmark test. However, both methods rely on the standard JavaScript Array prototype. **Special JS Features or Syntax** Neither `find` nor `findIndex` uses any special JavaScript features or syntax that requires additional explanation. **Other Alternatives** If you needed to find an element in an array based on a property, but without using an object as a comparison value (e.g., comparing two numbers), other options would be: * Using the `includes()` method and checking for a specific value in the array. * Using a custom loop or recursion to search through the array. However, these alternatives are not tested in this benchmark.
Related benchmarks:
find vs findIndex (Array prototype methods)
find vs findIndex (Array prototype methods) 22
find vs findindex with condition test
find vs findIndex (Array prototype methods) stop at first found
Comments
Confirm delete:
Do you really want to delete benchmark?