Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map looping2
(version: 0)
Comparing performance of:
forEach vs for of
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var map = new Map() for (let i = 0; i < 10000; i ++) { map.set(i, { text: 'a' + i }) }
Tests:
forEach
map.forEach(v => { v.text += 'f' })
for of
for (let [i, v] of map) { v.text += 'o' }
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 to understand what's being tested and the different approaches compared. **Benchmark Overview** The provided benchmark tests two JavaScript loops: `forEach` and `for...of`. The benchmark uses a simple `Map` data structure, where each key-value pair is created with an incrementing integer as the key and an object containing the string "a" followed by the key value as the value. **Loop Comparison** The benchmark compares two loop approaches: 1. **forEach**: This loop iterates over the values of the map using `map.forEach()`. The code inside the callback function increments the text property of each value. 2. **for...of**: This loop uses a for-of loop with an array-like context, where the array is created by iterating over the map's keys using `[i, v] of map`. The code inside the loop increments the text property of each value. **Pros and Cons** 1. **forEach**: * Pros: More concise and expressive way to iterate over values. * Cons: May be slower due to the callback function overhead. 2. **for...of**: * Pros: Can be faster since it avoids the callback function overhead, and can provide more fine-grained control over iterations (e.g., iterating only over keys). * Cons: Requires a slightly different data structure (an array-like context) and may be less concise. **Library: None** There is no external library used in this benchmark. The `Map` data structure is a built-in JavaScript object. **Special JS Features/Syntax: For...of** The for-of loop uses the new syntax feature introduced in ECMAScript 2015 (ES6). This syntax allows iterating over arrays and other iterable objects using a simpler syntax than traditional for loops. **Alternatives** Other alternatives to test these loops could include: 1. **Traditional for loops**: Comparing performance with traditional for loops using `for (let i = 0; i < map.size; i++)`. 2. **Array.prototype.forEach()**: Testing the array iteration method, which is similar to the `forEach` loop but uses an array's built-in method. 3. **Generators**: Using a generator function to iterate over the map and comparing performance with traditional for loops. Keep in mind that these alternatives might require modifications to the benchmark setup and script preparation code to accurately reflect the differences between each iteration approach.
Related benchmarks:
Map vs Obj12332
Map looping
Map loop
Map -> Array
Comments
Confirm delete:
Do you really want to delete benchmark?