Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..of against Map.entries vs for..in 2
(version: 0)
Comparing performance of:
for..of Map.entries() vs Map.foreach vs for..of Map.entries() no destructuring
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({ length: 10000 }, () => { const n = Math.floor(Math.random() * 100) if (n > 26) { return n } else { return String.fromCharCode('a'.charCodeAt(0) + n); } }); var obj = new Map(); for (let i = 0; i < array.length; i++) { obj.set(i, array[i]); }
Tests:
for..of Map.entries()
for (const [key, val] of obj.entries()) { const foo = val + 'a'; }
Map.foreach
obj.forEach((val) => { const foo = val + 'a'; })
for..of Map.entries() no destructuring
for (const entry of obj.entries()) { const key = entry[0]; const val = entry[1]; const foo = val + 'a'; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for..of Map.entries()
Map.foreach
for..of Map.entries() no destructuring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for..of Map.entries()
5067.5 Ops/sec
Map.foreach
5146.8 Ops/sec
for..of Map.entries() no destructuring
5165.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of different approaches for iterating over a `Map` object in JavaScript. **Options Compared** Three options are compared: 1. **For...of with Map.entries()**: This approach uses the `for...of` loop with the `.entries()` method to iterate over the key-value pairs of the map. 2. **Map.foreach**: This is not a standard JavaScript method, but rather a custom implementation (likely provided by the test author) that iterates over the map using the `forEach()` method. 3. **For...of without destructuring**: This approach uses the same syntax as the first option, but omits the destructuring of the key-value pair into separate variables. **Pros and Cons** * **For...of with Map.entries()**: + Pros: concise and expressive syntax, easy to read and understand. + Cons: may not be optimized for performance in all browsers or situations. * **Map.foreach**: + Pros: potentially optimized for performance, as it's a custom implementation. + Cons: unclear whether the optimization is effective, and the syntax may be unfamiliar or harder to read than other options. * **For...of without destructuring**: + Pros: similar benefits to the first option (concise and expressive), with an additional layer of abstraction removed. + Cons: less readable for some developers, as it requires more manual memory management. **Library** The `Map` data structure is a built-in JavaScript object, introduced in ECMAScript 2015. It's similar to a hash table or dictionary, where key-value pairs are stored and retrieved using the `.get()` method. **Special JS Feature/Syntax** None of the options used any special JavaScript features or syntax beyond what's commonly known (ECMAScript 2015+). **Other Alternatives** For iterating over maps in JavaScript, other approaches could include: 1. **Using a for loop with an index**: `for (let i = 0; i < map.size; i++) { const key = map.keys().next().value; const value = map.get(key); }` 2. **Using Array.from() and map.entries()**: `const array = Array.from(map.values()); for (let i = 0; i < array.length; i++) { const value = array[i]; }` These alternatives would have different performance characteristics, readability, and ease of use compared to the options tested in this benchmark. **Benchmark Considerations** The test measures the execution speed of each option. However, other factors like memory usage, cache locality, or parallelization may also impact performance in certain scenarios. Keep in mind that browser-specific optimizations, implementation details, or version differences might affect the results of this benchmark.
Related benchmarks:
for..of against Object.entries vs for..in
for..of against Map.entries vs for..in
map foreach vs array lookup vs const of
map foreach vs array lookup vs map const of vs arr const of
Comments
Confirm delete:
Do you really want to delete benchmark?