Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf random
(version: 0)
Comparing performance of:
indexOf vs findIndex
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill({ id: 0 }); arr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000); var el = arr.findIndex(e => e.id === foo);
Tests:
indexOf
var res = arr.indexOf(el);
findIndex
var res = arr.findIndex(item => item.id === el.id);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
findIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Overview** The benchmark is designed to compare the performance of two JavaScript methods: `findIndex` and `indexOf`. Both methods are used to find the index of a specific element within an array, but they differ in their implementation details. **Options Compared** 1. **`findIndex`**: This method returns the index of the first element that satisfies the provided condition. If no elements satisfy the condition, it returns -1. 2. **`indexOf`**: This method returns the index of the first occurrence of the specified value within the array, or -1 if the value is not found. **Pros and Cons** * `findIndex`: + Pros: It allows for more flexibility in filtering elements based on a custom predicate function. However, it may have performance implications if the predicate function is computationally expensive. + Cons: It can be slower than `indexOf` because of the potential overhead introduced by the predicate function. * `indexOf`: + Pros: It's generally faster and more lightweight than `findIndex`, especially when searching for exact values within a small range of indices. + Cons: It has limitations in terms of filtering elements based on custom predicates. **Library/Functionality Used** In this benchmark, the `map()` function is used to create an array with random IDs. The `Math.random()` function generates a pseudo-random value, and the `fill()` method populates the array with a specific ID. **Special JS Feature/Syntax** No special JavaScript features or syntax are explicitly mentioned in the benchmark definition. **Other Alternatives** If you were looking for alternatives to these methods, here are a few options: * For searching exact values: You could use `Array.prototype.indexOf()` or `BinarySearch` algorithm. * For filtering elements based on custom predicates: Consider using `Array.prototype.filter()` or a library like Lodash with its `filterBy` function. **Benchmark Preparation Code** The provided preparation code creates an array of 15,000 elements, fills it with random IDs using `Math.floor(Math.random() * 15000)`, and then maps the array to create a new array where each element's ID is set to its original index. This step ensures that all elements in the array have unique IDs. **Individual Test Cases** The benchmark consists of two test cases: 1. **`indexOf`**: Tests the performance of `Array.prototype.indexOf()` with an exact value. 2. **`findIndex`**: Tests the performance of `Array.prototype.findIndex()` with a custom predicate function that checks for equality between the element's ID and the target ID. The benchmark results show the execution per second for each test case on Chrome 95, running on Windows Desktop. The `findIndex` test outperforms `indexOf`, which is not surprising given the overhead introduced by the custom predicate function in `findIndex`.
Related benchmarks:
findIndex vs indexOf on array of objs
findIndex vs indexOf - JavaScript performance
findIndex vs map & indexOf vs find
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?