Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS find vs indexOf u
(version: 0)
JS find vs indexOf
Comparing performance of:
Array.find vs Array.indexOf
Created:
3 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.find
const item = arr.find(item => item === 1E5);
Array.indexOf
const index = arr.indexOf(1E5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.find
Array.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
4980.7 Ops/sec
Array.indexOf
151867.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark compares two JavaScript methods for finding a specific element in an array: `Array.prototype.find()` and `Array.prototype.indexOf()`. The tests are designed to measure the performance difference between these two methods. **Script Preparation Code** The script preparation code creates an array `arr` with 100,000 elements, where each element is initialized to its index value (`i`). This array will be used as input for both tests. ```javascript var arr = []; var i = 0; while (i <= 1E5) { arr[i] = i++; } ``` This code creates a large array with a linear progression of values, which is expected to be present in the array during the benchmarking process. **Benchmark Definition** The `Benchmark Definition` section contains two test cases: 1. **Array.find**: This test case uses the `find()` method to find an element with a value equal to 100,000. ```javascript const item = arr.find(item => item === 1E5); ``` This method returns the first element in the array that satisfies the provided condition. 2. **Array.indexOf**: This test case uses the `indexOf()` method to find the index of an element with a value equal to 100,000. ```javascript const index = arr.indexOf(1E5); ``` This method returns the index of the first occurrence of the specified value in the array. **Options Compared** The benchmark compares two options: 1. **Array.prototype.find()**: This method uses a callback function to specify the condition for finding an element. 2. **Array.prototype.indexOf()**: This method uses a direct comparison to find the index of an element. **Pros and Cons** * **Array.prototype.find():** + Pros: - Returns the first matching element, which can be useful in certain scenarios. - Less prone to off-by-one errors compared to `indexOf()`. + Cons: - Generally slower than `indexOf()` due to its callback-based approach. * **Array.prototype.indexOf():** + Pros: - Faster than `find()` since it uses a direct comparison. - Returns the index of the first occurrence, which can be useful in certain scenarios. + Cons: - May return an incorrect result if the value is not present in the array (returns -1). - More prone to off-by-one errors due to its indexing nature. **Library and Special JS Features** There are no external libraries used in this benchmark. However, it's worth noting that `find()` was introduced in ECMAScript 2015 (ES6), so it may not be supported in older browsers or environments. **Other Alternatives** If you're interested in exploring other methods for finding elements in an array, here are a few alternatives: 1. **Array.prototype.some()**: Returns `true` if at least one element satisfies the condition. 2. **Array.prototype every()**: Returns `true` if all elements satisfy the condition. 3. **Manual loop**: You can use a manual loop to iterate through the array and find the desired element. Keep in mind that these alternatives may have different performance characteristics compared to `find()` and `indexOf()`.
Related benchmarks:
JS find vs indexOf
JS find vs indexOf 2
find vs indexOf (Array prototype methods)
yabadabadoor
Comments
Confirm delete:
Do you really want to delete benchmark?