Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS find vs indexOf
(version: 0)
JS find vs indexOf
Comparing performance of:
Array.find vs Array.indexOf
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.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:
26 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
2994.5 Ops/sec
Array.indexOf
76400.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark definition and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The test is measuring the performance difference between two JavaScript methods: 1. `arr.find(item => item == 1E5)`: This method uses a callback function to find an element in the array that matches the condition. 2. `arr.indexOf(1E5)`: This method returns the index of the first occurrence of the specified value (in this case, `1E5`) in the array. **Options Compared** The benchmark is comparing two approaches: * `Array.prototype.find()` * `Array.prototype.indexOf()` **Pros and Cons of Each Approach** * **`arr.find()`**: This method uses a callback function to find an element that matches the condition. The pros are: + More expressive and readable code, as it clearly conveys the intent of finding a specific value. + Can be more efficient than `indexOf()` if the array is already sorted or has some other property that can aid in the search. * However, the cons are: + May be slower than `indexOf()`, especially for larger arrays, since it needs to iterate over each element and perform the callback function. + Requires the use of a callback function, which can add complexity to the code. * **`arr.indexOf()`**: This method returns the index of the first occurrence of the specified value in the array. The pros are: + Typically faster than `find()`, since it only needs to scan through the array until it finds the match. + Often used when you need to perform additional operations on the found element or its index. * However, the cons are: + Returns `-1` if the value is not found, which may require additional checks in the code. + May return a non-zero index if there are duplicate values. **Library and Its Purpose** In this case, there is no explicitly mentioned library being used. The `Array.prototype.find()` and `Array.prototype.indexOf()` methods are built-in to the JavaScript language. **Special JS Feature or Syntax** There is a special syntax being used in the benchmark: `1E5`. This represents a scientific notation for the number 10000, which is equivalent to `10**4` in regular arithmetic. This syntax is commonly used in JavaScript to represent large numbers more concisely. **Other Alternatives** If you wanted to explore other approaches, here are some alternatives: * For finding an element: You could use `Array.prototype.some()` or `Array.prototype.every()`, which return a boolean indicating whether at least one or all elements match the condition. * For indexing an element: You could use `Array.prototype.at()`, which returns the value at the specified index (if it exists). Keep in mind that these alternatives might have different performance characteristics and may not be as expressive or readable as the original `find()` and `indexOf()` methods. I hope this explanation helps you understand the benchmark!
Related benchmarks:
JS find vs indexOf 2
find vs indexOf (Array prototype methods)
JS find vs indexOf u
yabadabadoor
Comments
Confirm delete:
Do you really want to delete benchmark?