Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ELLHVRTqäU
(version: 0)
Comparing performance of:
findIndex vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill({ id: 0, name:'joseph', age: 20, children: 5, alien: false, createdAt: '2024-1-1', height: '180cm', age2: 30, age3: 40, age4: 50, age5: 120 }); arr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000); var bar = arr[foo];
Tests:
findIndex
var index = arr.findIndex((num) => num === foo);
indexOf
var 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:
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/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
27210.6 Ops/sec
indexOf
5018443.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and other considerations. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark definition. It contains two test cases: 1. `findIndex` 2. `indexOf` Both test cases use an array `arr` with 15,000 elements, each containing various properties (e.g., `id`, `name`, `age`, etc.). The array is filled with random data. **Script Preparation Code** The script preparation code generates the array `arr` and populates it with random data. It also sets up a variable `foo` to be used later in the benchmark tests. ```javascript var arr = new Array(15000); arr.fill({ id: 0, name:'joseph', age: 20, children: 5, alien: false, createdAt: '2024-1-1', height: '180cm', age2: 30, age3: 40, age4: 50, age5: 120 }); arr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000); var bar = arr[foo]; ``` **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** The two test cases are: 1. `findIndex` 2. `indexOf` Both tests use the `arr` array and the variable `foo`. The differences between the two tests lie in how they find the index of `foo` in the array: * **findIndex**: Uses the `findIndex()` method, which returns the index of the first element in the array that satisfies the provided condition. * **indexOf**: Uses the `indexOf()` method, which returns the index of the first occurrence of the specified value in the array. If no match is found, it returns -1. **Pros and Cons** Here are some pros and cons of each approach: * **findIndex**: * Pros: It's more flexible than `indexOf`, as it allows finding elements based on any condition (not just exact equality). However, it may have higher performance overhead due to the use of a callback function. * Cons: May be slower for large arrays since it iterates over each element until it finds a match. This can lead to unnecessary computations if `foo` is not in the array. * **indexOf**: * Pros: Generally faster than `findIndex`, especially when dealing with exact equality, as it uses a direct index lookup (hash table-based). * Cons: Less flexible than `findIndex` since it only works for exact equality. It also returns -1 if no match is found. **Library and Special JS Features** There are no libraries mentioned in the benchmark definition. However, there are some special JavaScript features being used: * **Map()**: Used to populate the array `arr` with random data. * **Math.random()**: Used to generate a random value for the variable `foo`. * **Arrow functions (=>)**: Used to define small anonymous functions in the script preparation code. **Other Alternatives** For large-scale benchmarking and performance comparison, it's essential to consider other factors beyond just algorithmic differences: * **Array size**: Increasing the array size can affect performance, as most algorithms scale linearly or quadratically with array size. * **Data distribution**: The distribution of data in the array (e.g., uniform vs. skewed) can impact performance. * **Browser/Platform-specific optimizations**: Different browsers and platforms may optimize for specific use cases or have inherent limitations that affect performance. Keep these factors in mind when designing benchmarks, especially if you plan to compare different approaches across various browsers, platforms, or hardware configurations.
Related benchmarks:
Fill array with random integers
Right shift VS Divide and floor
Right shift VS Divide and floor 2
Test Alex hoho
Comments
Confirm delete:
Do you really want to delete benchmark?