Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs map & indexOf
(version: 0)
Comparing performance of:
findIndex vs map and indexOf
Created:
7 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);
Tests:
findIndex
var index = arr.indexOf(foo);
map and indexOf
var index = arr.map(o => o.id).indexOf(foo);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
map and indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
328196.2 Ops/sec
map and indexOf
14329.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested on MeasureThat.net. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: 1. Using `indexOf` (method 1) 2. Using `map` with `indexOf` (method 2) Both methods aim to find the index of a specific value (`foo`) within an array (`arr`). The size of the array is fixed at 15,000 elements. **Script Preparation Code** The script preparation code creates an array `arr` filled with objects having an `id` property set to 0. It then maps each element in the array to its `id`, assigning it a value equal to the index of the original element (`el.id = idx`). This step is not directly related to finding the index of a specific value. **Individual Test Cases** There are two test cases: 1. **findIndex**: This test case uses `indexOf` to find the index of `foo` within the array. 2. **map and indexOf**: This test case maps each element in the array to its `id`, then uses `indexOf` to find the index of `foo` among the mapped values. **Pros and Cons** **Method 1: Using `indexOf`** Pros: * Simple and straightforward approach * Easy to understand and implement Cons: * `indexOf` is a linear search algorithm, which means its performance scales with the size of the array. * It may not be as efficient for large datasets. **Method 2: Using `map` with `indexOf`** Pros: * Combining `map` and `indexOf` allows for a potential optimization, as the mapped values can be used to skip unnecessary comparisons in `indexOf`. * This approach takes advantage of the fact that the mapped values are already sorted. Cons: * The combined approach introduces an additional function call (`map`) and requires more memory to store the intermediate results. * If the mapping process is slow or expensive, it may negate any potential performance benefits. **Library Used** In this benchmark, there is no explicit library mentioned. However, `map` is a built-in JavaScript method that's part of the ECMAScript standard. **Special JS Feature/Syntax** There are two special features in this benchmark: 1. **Destructuring assignment**: The mapping function uses an array destructuring assignment (`o => o.id`) to extract the value of interest. 2. **Arbitrary values**: The `foo` variable is assigned a random index value using `Math.floor(Math.random() * 15000)`. This ensures that the test case is not just about finding the index of a specific value, but also about searching for an arbitrary value. **Other Alternatives** If you're interested in exploring alternative approaches or optimizations, here are some ideas: * Using a custom algorithm or data structure (e.g., binary search, hash tables) * Compiling or JIT-ting the JavaScript code to optimize performance * Using parallel processing or multi-threading to take advantage of multiple CPU cores * Optimizing the mapping function for better performance Keep in mind that these alternatives may add complexity and require more effort to implement. However, they can be worth exploring if you're looking to squeeze every last bit of performance out of your JavaScript code.
Related benchmarks:
findIndex vs indexOf - JavaScript performance
findIndex vs map & indexOf vs find
Comparing findIndex with map & indexOf
findIndex vs IndexOf + map
Comments
Confirm delete:
Do you really want to delete benchmark?