Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get index with indexOf or with map
(version: 0)
Comparing performance of:
indexOf vs map
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({ index: i, text: `${i}` }); } var map = new Map(); array.forEach((item, index) => map.set(item, index));
Tests:
indexOf
const toFindIndex = Math.floor(Math.random() * 300) array.indexOf(array.indexOf((v) => v === v[toFindIndex]));
map
const toFindIndex = Math.floor(Math.random() * 300) map.get(toFindIndex)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
3420063.5 Ops/sec
map
7279314.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and its various components to explain what's being tested. **Benchmark Definition** The benchmark is defined by two scripts: 1. **Script Preparation Code**: This code creates an array of 300 objects, each with `index` and `text` properties. It then creates a new Map (`map`) and populates it with the same data as the array, using the `forEach()` method to iterate over the array and set the `index` property for each item in the map. 2. **Html Preparation Code**: This field is empty, which means that no HTML code is generated or executed as part of this benchmark. The purpose of these scripts is to provide a setup for the test cases that follow. **Test Cases** There are two individual test cases: 1. **`indexOf` Test Case**: This test case uses the `array.indexOf()` method to find the index of an item in the array, where the `index` property is randomly generated. 2. **`map` Test Case**: This test case uses the Map (`map`) created earlier to retrieve the value associated with a given key (in this case, a random `index`). **Options Compared** The two test cases are comparing the performance of using `array.indexOf()` versus using the Map data structure (`map.get()`). The Map approach is likely expected to be faster because it allows for O(1) lookups (i.e., constant-time lookup), whereas `array.indexOf()` has a time complexity of O(n). **Pros and Cons** * **`indexOf` Method**: + Pros: Simple, widely supported, and well-documented. + Cons: Slow for large datasets due to its linear search nature. * **Map Approach (`map.get()`)**: + Pros: Fast lookups (O(1)) and efficient memory usage. + Cons: May require additional setup and understanding of Map data structures. In general, the Map approach is expected to perform better than `array.indexOf()` for large datasets, but it may require more expertise to set up and use effectively. **Libraries Used** None are explicitly mentioned in this benchmark definition. **Special JS Features or Syntax** There is no mention of special JavaScript features or syntax in this benchmark. The test cases only use basic JavaScript concepts and data structures (arrays and Maps). **Alternatives** Other alternatives for searching arrays or Maps could include: * Using `Array.prototype.findIndex()` instead of `array.indexOf()`. * Implementing a custom binary search algorithm for large datasets. * Utilizing other data structures, such as a Trie, to speed up lookups. Keep in mind that these alternatives may have their own trade-offs and requirements, and the Map approach (`map.get()`) is likely designed to be efficient and straightforward to use.
Related benchmarks:
for vs foreach vs map
for vs map
for vs foreach vs map 2
Some vs. findIndex 3
Comments
Confirm delete:
Do you really want to delete benchmark?