Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf - JavaScript performance (fixed 2)
(version: 0)
Comparing performance of:
findIndex vs indexOf vs findIndex (again) vs indexOf (again)
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill(null); arr = arr.map((el, idx) => ({ id: idx })); var randomEl = arr[Math.floor(Math.random() * 15000)]; var randomId = randomEl.id;
Tests:
findIndex
var index = arr.findIndex((el) => el.id === randomId);
indexOf
var index = arr.indexOf(randomEl);
findIndex (again)
var index = arr.findIndex((el) => el.id === randomId);
indexOf (again)
var index = arr.indexOf(randomEl);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
findIndex
indexOf
findIndex (again)
indexOf (again)
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
1616.5 Ops/sec
indexOf
677903.3 Ops/sec
findIndex (again)
1555.7 Ops/sec
indexOf (again)
676900.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, including the provided benchmark that compares the performance of `findIndex` and `indexOf` methods. **Benchmark Definition** The benchmark definition represents a single test case for the comparison. In this case, we have four test cases: * `findIndex`: Uses the `findIndex` method to find an element in the array. * `indexOf`: Uses the `indexOf` method to find the index of an element in the array. * `findIndex (again)`: Same as the previous one, but with a duplicate name. * `indexOf (again)`: Same as the previous one, but with a duplicate name. The benchmark preparation code creates a large array (`arr`) and fills it with null values. Then, it maps over the array to create an object with an `id` property. A random element is chosen from the array, and its `id` is stored in a variable (`randomId`). The test case uses these variables to find the index of the random element using either `findIndex` or `indexOf`. **Options Compared** The benchmark compares two options: 1. **`findIndex`**: A method that returns the index of the first element in the array that satisfies the provided callback function. 2. **`indexOf`**: A method that returns the index of the first occurrence of the specified value, or -1 if not found. **Pros and Cons** * `findIndex`: + Pros: Can be more efficient than `indexOf` for certain use cases, as it uses a single loop iteration to find the element. + Cons: Can be slower than `indexOf` for large arrays, since it requires searching through the entire array to find the first matching element. * `indexOf`: + Pros: Can be faster than `findIndex` for very large arrays, since it only searches up to a specified value. + Cons: Requires specifying a value to search for, which can lead to errors if the wrong value is used. **Library and Purpose** The benchmark does not use any external libraries. However, it uses some built-in JavaScript features, such as: * `Array.prototype.findIndex`: A method that returns the index of the first element in the array that satisfies the provided callback function. * `Array.prototype.indexOf`: A method that returns the index of the first occurrence of the specified value. **Special JS Feature or Syntax** The benchmark uses the following special JavaScript feature: * `map()`: A method that creates a new array by applying a transformation to each element in the original array. * `lambda expression` (`(el, idx) => ({ id: idx })`): An anonymous function that takes an element and its index as arguments and returns an object with an `id` property. **Other Alternatives** If you want to write similar benchmarks for other JavaScript methods or features, consider using MeasureThat.net's benchmarking framework. Alternatively, you can use a testing library like Jest or Mocha to write unit tests for your code. Here is an example of how you could rewrite the `findIndex` test case using Jest: ```javascript const arr = new Array(15000).fill(null); arr.map((el, idx) => ({ id: idx })); test('findIndex', () => { const randomEl = arr[Math.floor(Math.random() * 15000)]; const randomId = randomEl.id; const index = arr.findIndex((el) => el.id === randomId); expect(index).toBeGreaterThanOrEqual(0); }); ``` Note that this is just an example, and you should adjust the test case to fit your specific use case.
Related benchmarks:
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf - JavaScript performancedsadsadas
findIndex vs indexOf vs includes - JavaScript performance
findIndex vs IndexOf + map
findIndex vs indexOf - JavaScript performance (fixed - 4 items)
Comments
Confirm delete:
Do you really want to delete benchmark?