Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS find vs indexOf 2
(version: 0)
JS find vs indexOf
Comparing performance of:
Array.find vs Array.indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 30000) arr[i] = i++;
Tests:
Array.find
const item = arr.find(item => item === 23000);
Array.indexOf
const index = arr.indexOf(23000);
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
41071.3 Ops/sec
Array.indexOf
672176.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two JavaScript methods: `find` and `indexOf`. Both methods are used to find an element in an array, but they work differently. **Method `find`** * Purpose: Find the first occurrence of a specified value (in this case, 23000) in the array. * Approach: The method returns the index of the first element that matches the condition. In this case, it will return the index of the element with value 23000. **Method `indexOf`** * Purpose: Find the index of the first occurrence of a specified value (in this case, 23000) in the array. * Approach: The method returns the index of the first element that matches the condition. In this case, it will return the index of the element with value 23000. **Pros and Cons** * `find`: + Pros: More concise and expressive way to find elements in an array. + Cons: May be slower than `indexOf` for large arrays, as it has to iterate through the entire array to find the first match. * `indexOf`: + Pros: Faster than `find` for large arrays, as it uses a linear search algorithm that stops as soon as it finds a match. + Cons: More verbose and less expressive than `find`. **Library and Special Features** In this benchmark, there is no external library used. However, it's worth noting that the use of `let` and arrow functions (`=>`) in the script preparation code suggests that modern JavaScript features are being used. **Other Alternatives** If you wanted to test other methods for finding elements in an array, some alternatives could include: * Using `splice()` to find and remove the element * Using a custom function with a loop * Using a library like Lodash However, these alternatives would likely have different performance characteristics and may not be as efficient or concise as `find` and `indexOf`. **Benchmark Preparation Code** The script preparation code creates an array of 30,000 elements and assigns values from 0 to 29,999. This is done to ensure that the benchmark can test finding large numbers in the array. **Individual Test Cases** Each test case runs a separate benchmark with either `find` or `indexOf`, using the same input data (the array created by the script preparation code). The resulting execution times are compared across different browsers and devices.
Related benchmarks:
JS find vs indexOf
JS find vs indexOf 3
JS find vs indexOf 4
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?