Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs indexOf for simple array 2
(version: 0)
Comparing performance of:
findIndex vs indexOf
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for(let i = 0; i < 15000; i++) {arr[i] = i;} var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
const index = arr.findIndex((element) => element === foo);
indexOf
const index = arr.indexOf(foo);
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:
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 provided benchmark and explain what's being tested. **What is being tested?** The provided JSON represents a JavaScript microbenchmark that compares the performance of two methods to find an element in an array: `Array.prototype.findIndex()` and `Array.prototype.indexOf()`. The test case uses a simple array of 15,000 elements, each with a unique value between 0 and 14,999. A random index is generated, and the benchmark measures how many times each method can execute within a second. **Options being compared:** The two options being compared are: 1. `Array.prototype.findIndex()`: This method returns the index of the first element that satisfies the provided condition (in this case, finding an element equal to the randomly generated value). If no such element is found, it returns -1. 2. `Array.prototype.indexOf()`: This method returns the index of the first occurrence of a specified value in the array. If the value is not found, it returns -1. **Pros and Cons:** **`Array.prototype.findIndex()`** Pros: * More concise and expressive than `indexOf` * Can be used to find all indices that satisfy a condition (by using an iterator) Cons: * Less performant for small arrays or exact matches * Some older browsers may not support it **`Array.prototype.indexOf()`** Pros: * Widely supported across browsers and versions * More performant than `findIndex` for small arrays or exact matches Cons: * Less concise and less expressive than `findIndex` * Returns -1 if the value is not found, whereas `findIndex` returns -1 when no elements satisfy the condition **Library:** None of the methods in question use any external libraries. **Special JS feature/syntax:** The benchmark uses JavaScript's built-in functions and syntax. No special features or syntax are used. **Other considerations:** To provide a more accurate comparison, it would be helpful to consider other factors such as: * Array size distribution (e.g., is the array mostly contiguous or sparse?) * Data type and structure (e.g., floating-point numbers, strings, etc.) * Platform-specific optimizations or differences **Alternatives:** Other alternatives for finding an element in an array could include: 1. `Array.prototype.includes()`: This method returns a boolean indicating whether the specified value is present in the array. 2. Using `Array.prototype.some()` and `Array.prototype.filter()`: These methods can be used to find all indices that satisfy a condition, but may have different performance characteristics. Keep in mind that these alternatives may not be as concise or expressive as `findIndex` and `indexOf`, respectively.
Related benchmarks:
findIndex vs indexOf
findIndex vs indexOf on array of objs
indexOf vs findIndex with a simple case
findIndex vs indexOf - JavaScript performance
Comments
Confirm delete:
Do you really want to delete benchmark?