Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map iteration 2
(version: 0)
Comparing performance of:
entries() vs for of
Created:
4 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));
Tests:
entries()
let total = 0; for (const [key, value] of map) { total += value; }
for of
let total = 0, key, value; for ([key, value] of map.entries()) { total += value; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
entries()
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested. **Benchmark Definition** The `map iteration 2` benchmark is testing the performance of iterating over a Map object using two different approaches: `entries()` and `for of`. **Script Preparation Code** The script preparation code creates a large Map object, `obj`, with 26 entries (keys-value pairs) and assigns it to a variable `map`. The `Map` constructor is used to create the map from an array of key-value pairs created using `Object.entries(obj)`. **Html Preparation Code** There is no HTML preparation code provided for this benchmark. **Individual Test Cases** ### 1. `entries()` This test case uses the `entries()` method to iterate over the Map object and calculates the sum of all values in the map. **Library Used:** None **Pros:** * Efficient way to iterate over a Map, as it returns an iterator that yields key-value pairs. * Can be faster than other methods for large datasets. **Cons:** * May not be suitable for older browsers or systems that don't support modern iterators. * Can be less readable and maintainable compared to traditional `for` loops. ### 2. `for of` This test case uses a traditional `for...of` loop to iterate over the Map object's entries, also calculating the sum of all values in the map. **Library Used:** None **Pros:** * More readable and maintainable than using iterators or `entries()` methods. * Can be suitable for older browsers or systems that don't support modern iterators. * Provides more control over iteration order (if needed). **Cons:** * May not be as efficient as using `entries()` or other iterator-based approaches for large datasets. **Special JS Feature/Syntax:** None mentioned in the provided benchmark. **Other Alternatives:** 1. **Array.prototype.forEach()**: This method can also be used to iterate over a Map's entries, although it may have performance implications for very large datasets. 2. **Map.entries().forEach()**: Similar to `Array.prototype.forEach()`, but using the Map's iterator-based approach. **Benchmark Results** The latest benchmark results show that the `entries()` approach is slightly faster than the traditional `for...of` loop, with an execution rate of 3427626.0 executions per second compared to 3425302.5 executions per second for the `for of` loop on a Mac OS X 10.15.7 system running Chrome 96. In summary, this benchmark is testing the performance of two common ways to iterate over a Map object in JavaScript: using the `entries()` method and traditional `for...of` loops. The results indicate that `entries()` may be slightly faster for this specific use case.
Related benchmarks:
Map Value Iteration
Object iteration vs Map iteration
Object iteration vs Map iteration new
Object iteration vs Map iteration V2
Object iteration vs Map iteration with deep cloning
Comments
Confirm delete:
Do you really want to delete benchmark?