Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.indexOf vs Array.prototype.findIndex
(version: 0)
Comparing performance of:
Array.prototype.indexOf vs Array.prototype.findIndex
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill({ id: 0 }); arr = arr.map((el, idx) => idx); var foo = Math.floor(Math.random() * 15000);
Tests:
Array.prototype.indexOf
var index = arr.indexOf(foo);
Array.prototype.findIndex
var index = arr.findIndex(el => el === foo);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.indexOf
Array.prototype.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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided JSON represents a benchmark test comparing two methods for finding an element in an array: `indexOf` and `findIndex`. The test is designed to measure which method performs better in terms of speed. **Options Compared** There are two options being compared: 1. **`Array.prototype.indexOf`**: This method returns the index of the first occurrence of a specified value within the array, or -1 if the value is not found. 2. **`Array.prototype.findIndex`**: This method returns the index of the first element in an array that satisfies the provided testing function. **Pros and Cons** * `indexOf`: + Pros: Simple to implement, widely supported across browsers. + Cons: May have issues with NaN (Not a Number) values, as it will return -1, which might not be what you expect. It also has a time complexity of O(n), where n is the length of the array. * `findIndex`: + Pros: More flexible than `indexOf`, allows for a custom callback function to test each element in the array. It has a time complexity of O(n) as well, but it's generally faster than `indexOf` because it stops searching once it finds the first match. + Cons: Requires JavaScript 5 or later to work. The callback function should return true for the desired element and false otherwise. **Library and Special JS Features** There are no specific libraries being used in this benchmark, but there is a use of `Math.random()` to generate a random index for the array elements. No special JS features are being tested in this benchmark. **Other Considerations** When writing benchmarks like this one, it's essential to consider factors such as: * Array size and distribution: The test uses an array of 15,000 elements. You should adjust the size based on your specific use case. * Data type: The test only uses integers for the array values. Using more complex data types (e.g., objects or arrays) can affect performance. * Browser support: Ensure that you're testing in multiple browsers to get a representative result. **Alternatives** Other alternatives to measure performance differences between `indexOf` and `findIndex` include: * Using `Array.prototype.includes()` instead of `indexOf`, as it's more concise and readable. * Creating your own custom function for finding elements in an array, which can be optimized for specific use cases. * Using a testing library like Benchmark.js or MicroBenchmark to automate benchmarking. By understanding the basics of these methods and their performance characteristics, you'll be better equipped to make informed decisions about when to use `indexOf` versus `findIndex` in your JavaScript applications.
Related benchmarks:
findIndex vs indexOf on array of objs
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf for simple array 2
Array.prototype.findIndex vs Array.prototype.map + Array.prototype.indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?