Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf - JavaScript performance (fixed - 4 items)
(version: 0)
Comparing performance of:
findIndex vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var count = 4; var arr = new Array(count); arr.fill(null); arr = arr.map((el, idx) => ({ id: idx })); var randomEl = arr[Math.floor(Math.random() * count)]; var randomId = randomEl.id;
Tests:
findIndex
var index = arr.findIndex((el) => el.id === randomId);
indexOf
var index = arr.indexOf(randomEl);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
3410205.2 Ops/sec
indexOf
9020249.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **What is being tested?** The provided benchmark measures the performance difference between two JavaScript methods: `Array.prototype.findIndex()` (also known as "find index") and `Array.prototype.indexOf()`. Both methods are used to find the index of a specific element in an array, but they have some differences in their behavior and performance. **Options being compared** There are two main options being compared: 1. **`Array.prototype.findIndex()`**: This method returns the index of the first element in the array that satisfies the provided testing function. If no elements satisfy the test, it returns -1. 2. **`Array.prototype.indexOf()`**: This method returns the index of the first occurrence of the specified value in the array. If the value is not found, it returns -1. **Pros and Cons** Here are some pros and cons of each approach: * `Array.prototype.findIndex()`: + Pros: - It can be more efficient when searching for a specific element that meets a certain condition. - It avoids unnecessary iterations over the array. + Cons: - It returns -1 if no elements satisfy the test, whereas `indexOf()` always returns a non-negative integer. * `Array.prototype.indexOf()`: + Pros: - It is more straightforward and easier to understand for simple cases. + Cons: - It may be less efficient than `findIndex()` when searching for an element that meets a specific condition, since it has to iterate over the entire array. **Library** There is no library being used in this benchmark. Both `Array.prototype.findIndex()` and `Array.prototype.indexOf()` are native JavaScript methods. **Special JS feature or syntax** The benchmark uses the `map()` method, which creates a new array with the results of applying a provided function to each element in an existing array. The `map()` method is a built-in JavaScript method that is supported by most modern browsers and Node.js environments. **Other alternatives** If you wanted to implement your own indexing algorithm, you could use a simple loop or recursion to find the index of an element in an array. However, these approaches would likely be slower and less efficient than using native methods like `findIndex()` and `indexOf()`.
Related benchmarks:
indexOf vs findIndex with a simple case
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf - JavaScript performancedsadsadas
findIndex vs indexOf vs includes - JavaScript performance
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?