Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs array
(version: 0)
Comparing performance of:
array vs map
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (var i=0; i<300; ++i) { array.push('00' + i); } function hasWithIndexOf(needle) { return array.indexOf(needle) !== -1; } var map = new Set(); array.forEach(item => map.add(item)); function hasWithMap(needle) { return map.has(needle); }
Tests:
array
for (var i=0; i<100; ++i) { hasWithIndexOf('404'); }
map
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
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 YaBrowser/25.2.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
23860.8 Ops/sec
map
10207052.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the provided JSON benchmark data. **Benchmark Definition** The provided JSON defines two benchmarks: 1. **map vs array**: This benchmark compares the performance of using `Set` and `indexOf()` to an array (`push()` and `includes()` methods). The benchmark script creates an array with 300 elements, each represented as a string in the format "00" followed by a number from 0 to 299. Two functions are defined: `hasWithIndexOf(needle)` for the array approach and `hasWithMap(needle)` for the Set approach. The script initializes an empty Set (`map`) and populates it with elements from the array using `forEach()` method. The functions `hasWithIndexOf(needle)` and `hasWithMap(needle)` check if a given value is present in the array or Set, respectively. **Options Compared** The benchmark compares two approaches: * **Array Approach**: Using an array (`push()` and `includes()` methods) * **Set Approach**: Using a Set (`forEach()` method to populate it) **Pros and Cons of Each Approach** * **Array Approach**: + Pros: - Easier to implement for developers familiar with arrays. - No need to create a separate data structure (in this case, a Set). + Cons: - May be slower due to the overhead of searching through large arrays using `indexOf()`. * **Set Approach**: + Pros: - Can be faster for certain use cases, as Sets provide constant-time lookup and insertion. - No need to search through a list of elements (array). + Cons: - Requires creating a separate data structure (a Set), which can have additional memory overhead. **Other Considerations** * The benchmark does not consider other factors that might affect performance, such as array or Set resizing, cache locality, or parallel processing. * The `forEach()` method is used to populate the Set, but it's not clear if this is an optimal implementation for large datasets. **Library and Special JS Features** The benchmark uses the following JavaScript library: * None explicitly mentioned; however, `Set` is a built-in JavaScript object (introduced in ECMAScript 2015). There are no special JavaScript features used in this benchmark.
Related benchmarks:
indexOf vs map vs map2
indexOf vs map vs Set vs native Map
indexOf vs map on smaller arrays
indexOf vs map iterator
Array.findFirst vs Map.has
Comments
Confirm delete:
Do you really want to delete benchmark?