Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object iteration vs Map iteration with lambda functions
(version: 0)
Comparing performance of:
Map - for of kv vs Map - forEach vs Object.entries() - for of kv vs Object.entries() - forEach vs Object - for in vs repeat first test
Created:
3 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:
Map - for of kv
let total = 0; for (const [key, value] of map) { total += value; }
Map - forEach
let total = 0; map.forEach(value => { total += value; });
Object.entries() - for of kv
let total = 0; for (const [key, value] of Object.entries(obj)) { total += value; }
Object.entries() - forEach
let total = 0; Object.entries(obj).forEach(value => { total += value; });
Object - for in
let total = 0; for (const key in obj) { total += obj[key]; }
repeat first test
let total = 0; for (const [key, value] of map) { total += value; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Map - for of kv
Map - forEach
Object.entries() - for of kv
Object.entries() - forEach
Object - for in
repeat first test
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 dive into the benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance of three different ways to iterate over an object or map in JavaScript: 1. **Map iteration with `for...of`**: This method uses the `for...of` loop syntax to iterate over the key-value pairs of a Map. 2. **Map iteration with `forEach`**: This method uses the `forEach` method of the Map interface to iterate over its elements. 3. **Object iteration with `for...of`**: This method uses the `for...of` loop syntax to iterate over the key-value pairs of an object. 4. **Object iteration with `forEach`**: This method uses the `forEach` method of the Object prototype to iterate over its properties. **Options Compared** The benchmark compares the performance of these four methods: * Map iteration with `for...of` * Map iteration with `forEach` * Object iteration with `for...of` * Object iteration with `forEach` **Pros and Cons of Each Approach** 1. **Map Iteration with `for...of`**: * Pros: Efficient, concise syntax, and modern JavaScript. * Cons: Limited support in older browsers (e.g., Internet Explorer). 2. **Map Iteration with `forEach`**: * Pros: Wide browser support, including older versions of Chrome. * Cons: Less efficient than `for...of`, potentially slower due to function call overhead. 3. **Object Iteration with `for...of`**: * Pros: Efficient, concise syntax, and modern JavaScript. * Cons: Limited support in older browsers (e.g., Internet Explorer). 4. **Object Iteration with `forEach`**: * Pros: Wide browser support, including older versions of Chrome. * Cons: Less efficient than `for...of`, potentially slower due to function call overhead. **Other Considerations** * The benchmark uses a large object (`obj`) and Map (`map`) to ensure that the results are representative of real-world scenarios. * The test cases use lambda functions (i.e., anonymous functions) to iterate over the objects, which can impact performance in some browsers. * The `repeat first test` variant is likely included to provide a baseline for comparison, as it uses the same iteration method as the original test case. **Alternative Methods** Other methods for iterating over objects and maps in JavaScript include: 1. **Using `Object.keys()` or `Object.getOwnPropertyNames()`**: These methods return an array of property names, which can be used with a loop. 2. **Using `Array.prototype.map()`**: This method applies a transformation function to each element of an array (or Map) and returns a new array. 3. **Using `Array.prototype.forEach()`: This method calls a provided function once for each element in an array (or Map). Keep in mind that these alternatives may have different performance characteristics and use cases compared to the methods tested in this benchmark.
Related benchmarks:
Object iteration methods with maps, foreach, different access types, no mutation
Array.from vs Array.prototype.map
Array.forEach vs Object.keys().forEach
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
Map.forEach vs Array.forEach vs Array.from(Map.values()).forEach
Comments
Confirm delete:
Do you really want to delete benchmark?