Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map speed test 5
(version: 0)
Comparing performance of:
forEach vs for .. of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateTestMap() { const result = new Map; for (let i = 0; i < 1000000; ++i) { result.set(i, { a: i, b: i / 2, r: 0, }); } return result; }
Tests:
forEach
const map = generateTestMap(); map.forEach((x, i) => { x.r = x.a + x.b + i; });
for .. of
const map = generateTestMap(); for(const [i, x] of map.entries()) { x.r = x.a + x.b + i; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
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 JSON and explain what's being tested. **Benchmark Definition** The benchmark definition represents a JavaScript function that generates a large Map (a data structure similar to an object) with 1 million entries, each containing three properties: `a`, `b`, and `r`. The function is defined as: ```javascript function generateTestMap() { const result = new Map(); for (let i = 0; i < 1000000; ++i) { result.set(i, { a: i, b: i / 2, r: 0, }); } return result; } ``` This function creates a large Map by setting each entry with an integer key `i` and an object value containing the corresponding values for `a`, `b`, and `r`. **Benchmark Options Compared** The benchmark compares two approaches to iterate over the Map: 1. **forEach**: This method iterates over the entries of the Map using the `forEach` loop: ```javascript map.forEach((x, i) => { x.r = x.a + x.b + i; }); ``` Pros: Easy to read and write, works well for small to medium-sized data sets. Cons: In JavaScript, `forEach` is not optimized for large datasets like this one. It will iterate over the entire array in memory, which can be slow. 2. **for .. of**: This method iterates over the entries of the Map using a traditional `for` loop with an expression that returns each entry: ```javascript for (const [i, x] of map.entries()) { x.r = x.a + x.b + i; } ``` Pros: More memory-efficient than `forEach`, as it only accesses each entry once. It's also optimized for large datasets. Cons: Can be less readable and more complex to write than `forEach`. **Library and Special JS Features** In this benchmark, the following library is used: * None explicitly. However, the JavaScript built-in features are being utilized (Map data structure), which is quite standard in the programming language itself. No special JavaScript features or syntaxes are required for these two approaches to work. **Other Considerations** When running benchmarks like this one, it's essential to consider factors such as: * **Cache performance**: Modern browsers and CPUs have various cache hierarchies that can affect execution speed. Optimizing code to minimize cache misses is crucial. * **Memory allocation**: Creating large datasets like the Map in this benchmark can cause memory allocation issues. Efficient data structures and algorithms can help mitigate these problems. * **Platform-specific optimizations**: Different platforms (e.g., desktop vs. mobile) and browsers may have unique performance characteristics that need to be accounted for when running benchmarks. **Alternatives** Some alternatives to consider for optimizing the `forEach` loop: 1. Using an `Array.from()` conversion to iterate over the Map entries, which can provide better cache locality: ```javascript const array = Array.from(map.entries()); array.forEach(([i, x]) => { x.r = x.a + x.b + i; }); ``` 2. Utilizing the `Map.prototype.forEach()` method with a callback function that only accesses the desired properties: ```javascript map.forEach((x) => { x.r = x.a + x.b; }); ``` However, it's essential to remember that even with these optimizations, modern JavaScript engines and browsers are capable of providing good performance for many workloads.
Related benchmarks:
Map vs Array vs Object vs Set add item speed in 50000 iters 2
Performance of JavaScript .forEach, for in
Performance of JavaScript .forEach, for in v3
Map VS Set by Tonkhao
Comments
Confirm delete:
Do you really want to delete benchmark?