Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object iteration vs Map iteration 123
(version: 0)
Comparing performance of:
array vs map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, u: 21, v: 22, w: 23, x: 24, y: 25, z: 26 }; var map = new Map(Object.entries(obj)); var arr = Object.values(obj);
Tests:
array
let a = 0; arr.forEach((v) => { a += v; });
map
let a = 0; map.forEach((v, k) => { a += v; });
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:
11 months ago
)
User agent:
Mozilla/5.0 (Android 14; Mobile; rv:138.0) Gecko/138.0 Firefox/138.0
Browser/OS:
Firefox Mobile 138 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
2943494.2 Ops/sec
map
2844834.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is tested in the provided JSON, comparing different options and their pros and cons. **Benchmark Definition** The benchmark measures the performance of iterating over an array versus using a Map to iterate over the same data. **Options Compared** There are two main options compared: 1. **Array Iteration**: Using `arr.forEach()` to iterate over the array. 2. **Map Iteration**: Using `map.forEach()` to iterate over the Map. **Pros and Cons of Each Approach** ### Array Iteration (`arr.forEach()`) * Pros: + Wide support across browsers and platforms. + Easy to read and understand, as it's a straightforward iteration process. * Cons: + Can be slower for large datasets due to the need to access array elements sequentially. ### Map Iteration (`map.forEach()`) * Pros: + Generally faster than array iteration for large datasets, as it can take advantage of caching and more efficient memory management. + Allows for easy iteration over both key-value pairs (as in this benchmark) and values only. * Cons: + Less supported across older browsers and platforms, which may be a limitation. **Library Used** In the provided JSON, `Object.entries()` is used to create an array of key-value pairs from the original object. This function is part of the ECMAScript standard (ES6+) and is widely supported in modern browsers. It serves as a bridge between the original object and the Map data structure. **Special JS Feature or Syntax** In this benchmark, `const` is used to declare variables, which is the default keyword for declaring constant variables in JavaScript, starting from ECMAScript 2015 (ES6). This syntax is widely supported across browsers and platforms. **Other Considerations** * **Browser Support**: The benchmark uses Chrome 116 on a Mac OS X 10.15.7 platform. Other browsers may not support Map iteration or have different performance characteristics. * **Hardware and Software Configuration**: The benchmark's results are affected by the hardware and software configuration of the test device. This includes factors like processor speed, memory allocation, and caching. **Alternatives** If you need to compare other iteration methods, consider these alternatives: 1. **Array.prototype.forEach.call()**: Similar to `arr.forEach()`, but uses a more traditional, non-async approach. 2. **Array.prototype.map()**: Iterates over the array using a callback function, which can be useful for transforming data. 3. **Set Iteration**: If you need to iterate over unique values only, consider using a Set and iterating over its `values()` method. Keep in mind that these alternatives might have different performance characteristics or limitations compared to the original array iteration approach used in this benchmark.
Related benchmarks:
Map Value Iteration
Object.values vs array index loop
Object iteration vs Map iteration new
Object iteration vs Map iteration with deep cloning
Comments
Confirm delete:
Do you really want to delete benchmark?