Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs map vs Set vs native Map
(version: 0)
Comparing performance of:
indexOf vs map vs set vs map2
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (var i=0; i<3000; ++i) { array.push('00' + i); } function hasWithIndexOf(needle) { return array.indexOf(needle) !== -1; } var map = {}; array.forEach(item => map[item] = true); function hasWithMap(needle) { return needle in map; } var set = new Set(array); function hasWithSet(needle) { return set.has(needle); } var map2 = new Map(array.map(item => [item, true])); function hasWithMap2(needle) { return map2.has(needle); }
Tests:
indexOf
for (var i=0; i<100; ++i) { hasWithIndexOf('404'); }
map
for (var i=0; i<100; ++i) { hasWithMap('404'); }
set
for (var i=0; i<100; ++i) { hasWithSet('404'); }
map2
for (var i=0; i<100; ++i) { hasWithMap2('404'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
indexOf
map
set
map2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
2914.0 Ops/sec
map
8589416.0 Ops/sec
set
9664027.0 Ops/sec
map2
10097610.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of four different approaches to check if an element exists in an array: `indexOf`, `map`, `Set`, and `Map`. The tests are run on a fixed array of 3000 elements, with each test checking for the presence of a specific string ("404") using each approach. **Approaches Compared** 1. **`indexOf`**: This method uses the built-in `indexOf` function to search for an element in the array. 2. **`map`**: In this approach, the `array.forEach()` method is used to create a new object (`map`) where each key corresponds to an element in the original array. The test then checks if the string "404" exists as a key in this map. 3. **`Set`**: This approach creates a new `Set` object from the array and then uses the `has()` method to check if the string "404" is present in the set. 4. **`Map`**: Similar to the `map` approach, but instead of using an object, a `Map` object is created where each key corresponds to an element in the original array. **Pros and Cons of Each Approach** 1. **`indexOf`**: * Pros: Fastest execution time (due to built-in implementation). * Cons: May not be optimized for large arrays or specific use cases. 2. **`map`**: * Pros: Can be useful when working with objects where the key is also relevant (e.g., `map[key] = value`). * Cons: Slower execution time compared to `indexOf` due to object creation and iteration. 3. **`Set`**: * Pros: Fast lookup times, making it suitable for large datasets. * Cons: May not be optimized for specific use cases where the string is known in advance (e.g., "404"). 4. **`Map`**: * Pros: Similar to `map`, but with a more efficient data structure. * Cons: Still slower than `indexOf` due to object creation and iteration. **Other Considerations** * The use of `forEach()` instead of `for...of` or other iteration methods may impact performance for very large arrays. * The fixed array size of 3000 elements might not accurately represent real-world scenarios, where data sizes can vary greatly. **Alternative Approaches** 1. **Using a regular expression**: You could use a regular expression to search for the string "404" in the array. This approach would likely be slower than `indexOf`, but it provides more flexibility and can be useful in certain situations. 2. **Custom implementation**: Depending on your specific requirements, you might want to implement a custom algorithm or data structure that suits your needs better. In summary, the benchmark compares four different approaches to check if an element exists in an array. While `indexOf` is the fastest, it may not be optimized for large arrays or specific use cases. The other approaches have their pros and cons, and alternative methods like regular expressions or custom implementations might be suitable depending on your needs.
Related benchmarks:
for vs map
for vs foreach vs map 2
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?