Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map loop
(version: 0)
Comparing performance of:
forEach vs for of vs obj keys
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 }) } var obj = [] for (let i = 0; i < 10000; i ++) { map[i] = { text: 'a' + i } }
Tests:
forEach
map.forEach(v => { v.text += 'f' })
for of
for (let v of map) { v.text += 'o' }
obj keys
const keys = Object.keys(obj) for (let i = 0; i < keys.length; i ++) { obj[keys[i]].text += 'k' }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
forEach
for of
obj keys
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 what's being tested in this benchmark. **Benchmark Overview** The test cases are designed to measure the performance of different JavaScript loops on a large dataset represented by two Maps: `map` and an array-like object `obj`. The goal is to compare the execution time of these three loop types: 1. `forEach` 2. `for...of` 3. Direct array indexing (`[i] = { text: 'a' + i }`) **Loop Comparisons** Here's a brief explanation of each loop type and their pros/cons: ### 1. `forEach` Pros: * Elegant syntax * No need to worry about the length of the collection Cons: * May incur additional overhead due to the function invocation mechanism (i.e., creating an internal function context) In this benchmark, `forEach` is executed on a large Map, which has a fixed number of entries. Since Maps are designed for fast lookups and iteration, the execution time should be relatively consistent. ### 2. `for...of` Pros: * Similar syntax to traditional loops * Can provide better performance due to caching and reduced function call overhead Cons: * Requires a compatible array-like object (in this case, `obj`) * May not work well with non-iterable objects In this benchmark, `for...of` is executed on both the Map (`map`) and the array-like object (`obj`). Since Maps are designed for fast iteration, the execution time should be relatively consistent. However, the performance may vary depending on the specific implementation of `for...of` in different browsers. ### 3. Direct Array Indexing Pros: * Can provide optimal performance due to direct access to memory * No additional function call overhead Cons: * Requires manual index management and can lead to off-by-one errors if not implemented carefully In this benchmark, direct array indexing is used to update the `text` property of each object in the Map. This approach can be optimized for performance but requires careful consideration of edge cases. **Library and Special Features** None of the provided benchmarks use any external libraries or special JavaScript features beyond what's part of the standard library. **Other Alternatives** To further compare the performance of these loop types, additional test cases could include: * Using `for...in` instead of `forEach` * Implementing custom iteration logic using a simple iterator function * Testing with different data sizes and distributions (e.g., random numbers, strings) * Comparing performance on various platforms (e.g., Node.js, web browsers) Keep in mind that the results may vary depending on the specific implementation, browser version, and system configuration.
Related benchmarks:
for vs map
Map looping
Map looping2
Map -> Array
Comments
Confirm delete:
Do you really want to delete benchmark?