Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object iteration vs Map iteration new
(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 Object - keys forech
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(function(value, key) { 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(function(value, key) { total += value; });
Object - for in
let total = 0; for (const key in obj) { total += obj[key]; }
Object - keys forech
let total = 0; Object.keys(obj).forEach(function(key) { value = obj[key]; 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
Object - keys forech
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):
I'll break down the provided JSON data and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Definition** The benchmark is designed to compare the performance of different ways to iterate over an object in JavaScript: using `Map` objects, `Object.entries()`, and traditional `for...in` loops. The test case uses a large object with 26 properties to simulate a real-world scenario. **Options Compared** There are four options being compared: 1. **Map - for of kv**: Iterating over a `Map` object using the `for...of` loop, which iterates over key-value pairs. 2. **Map - forEach**: Iterating over a `Map` object using the `forEach()` method. 3. **Object.entries() - for of kv**: Iterating over an object's entries using `Object.entries()`, and then using the `for...of` loop to iterate over the key-value pairs. 4. **Object.entries() - forEach**: Iterating over an object's entries using `Object.entries()`, and then using the `forEach()` method. **Pros and Cons** * **Map - for of kv**: Pros: * More efficient, as it directly accesses the map's keys and values without additional overhead. * Fewer iterations required, resulting in better performance. * Cons: Requires a `Map` object, which may not be suitable for all scenarios. * **Map - forEach**: Pros: * Easier to read and understand than traditional loops. * Works with any iterable data structure (not just maps). * Cons: May require additional overhead due to the method call, resulting in slower performance compared to `for...of`. * **Object.entries() - for of kv**: Pros: * More readable and intuitive than traditional loops. * Easier to understand for developers familiar with `Map` objects. * Cons: Requires an additional step to access the map's entries, which may introduce overhead. * **Object.entries() - forEach**: Pros: * Easy to read and understand, especially for those familiar with `forEach()` methods. * Works with any object, not just maps. * Cons: May be slower than traditional loops due to additional overhead from the method call. **Other Considerations** * The test uses a large object with 26 properties, which may not accurately represent real-world scenarios. A smaller dataset might provide more representative results. * The benchmark does not account for the number of iterations required to achieve a certain performance threshold, only the raw execution times. * Other factors that could affect performance, such as memory allocation and deallocation, are not considered in this benchmark. **Alternatives** * For smaller datasets or simpler scenarios, using traditional loops (e.g., `for...in`) might be sufficient and more readable. * Using libraries like Lodash or Ramda, which provide higher-order functions for iterating over data structures, could potentially outperform the options tested here in certain cases. By understanding the pros and cons of each option, developers can choose the most suitable approach for their specific use case and performance requirements.
Related benchmarks:
Array.find vs map to object then lookup.
iterating from a filled object VS iterating from a map
Object spread vs New map with string keys
Object.keys().length vs Map.size
Object spread vs new Map vs Object assign with complex data
Comments
Confirm delete:
Do you really want to delete benchmark?