Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs map with indexOf
(version: 0)
Comparing performance of:
Array index of vs Map index of
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (var i = 0; i < 300; ++i) { array.push({ 'id': i, 'name': 'test-' + i }); } function hasWithIndexOf(id) { return array.indexOf(i => i.id === id); } var map = array.map(i => i.id); function hasWithMap(id) { return map.indexOf(id); }
Tests:
Array index of
for (var i=0; i<100; ++i) { hasWithIndexOf('404'); }
Map index of
for (var i=0; i<100; ++i) { hasWithMap('404'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array index of
Map index of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array index of
198392.7 Ops/sec
Map index of
62270.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is an object that describes the test case. In this case, there are two test cases: 1. "indexOf vs map with indexOf" * This test compares the performance of using `indexOf` on an array versus mapping over the array to extract a specific property (`id`) and then finding the index of that value. 2. "Array index of" and "Map index of" The first two test cases have the same benchmark definition, but with different variations: * "Array index of" + Iterates 100 times, calling `hasWithIndexOf('404')` + This variation tests how fast it is to find an element using `indexOf` on a large array. * "Map index of" + Iterates 100 times, calling `hasWithMap('404')` + This variation tests how fast it is to find an element by mapping over the array and then finding the index in the resulting array. **Options compared** The two options being compared are: 1. Using `indexOf` on a large array 2. Mapping over the array, extracting the desired property (`id`), and then using `indexOf` again **Pros and Cons** * **Using `indexOf`**: Pros: + Fast and efficient for finding an exact match in a large array. + Can be useful when you need to find an element based on a specific value. * Cons: + Can be slow if the array is very large, since it has to search through the entire array. + May not be suitable if you're searching for a range of values or complex patterns. * **Mapping over the array and then using `indexOf`**: Pros: + Can be faster than using `indexOf` directly on a large array, since it avoids the overhead of searching through the entire array. + Allows for more flexibility in how you extract the desired value from each element. * Cons: + May have higher overhead due to the extra step of mapping over the array. + Can be slower if the mapping function is complex or expensive. **Library and purpose** The `hasWithIndexOf` and `hasWithMap` functions are using a library called Lodash. The purpose of these functions is to abstract away the implementation details of finding an element in the array, allowing you to focus on the logic of your test case. In particular, `hasWithIndexOf` takes an element (in this case, `i.id`) and returns a boolean indicating whether that element exists in the array. The Lodash `indexOf` function is used internally to perform the search. **Special JS feature or syntax** The `=>` symbol in the benchmark definition represents an arrow function. This is a shorthand way of defining small, anonymous functions. In this case, it's used to define a short function that extracts the `id` property from each object in the array. Overall, the benchmark is testing the performance difference between using `indexOf` directly on a large array versus mapping over the array and then finding the index with `indexOf`. The results can help identify which approach is faster for specific use cases.
Related benchmarks:
index vs lastindexofasdf
Array find with indexOf vs includes
array.includes vs array.indexOf
findIndex vs map with indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?