Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object iterations
(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
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]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Map - for of kv
Map - forEach
Object.entries() - for of kv
Object.entries() - forEach
Object - for in
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 world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition: Object iterations** The benchmark measures the performance of iterating over an object using different methods: `Object.entries()`, `for...of` with `Object.entries()`, `for...in` with direct property access, and `forEach`. The benchmark creates a large object with 26 properties and iterates over its entries or keys, adding up their values. **Options compared** 1. **Object.entries() - for of kv**: Uses the `for...of` loop with an iterator created from `Object.entries()` to iterate over the object's entries. 2. **Object.entries() - forEach**: Uses the `forEach` method on the iterator created from `Object.entries()` to iterate over the object's entries. 3. **Object - for in**: Uses a traditional `for...in` loop with direct property access (e.g., `obj[key]`) to iterate over the object's keys. **Pros and Cons of each approach** 1. **Object.entries() - for of kv**: * Pros: More concise, less memory usage, and potentially faster due to optimized iteration. * Cons: May not be as readable or familiar to developers who prefer traditional `for` loops. 2. **Object.entries() - forEach**: * Pros: Still concise, easy to read, and works well with existing codebases. * Cons: Less efficient than the `for...of` variant due to additional overhead from the `forEach` method. 3. **Object - for in**: * Pros: Widely supported, easy to understand, and suitable for iterating over objects with inherited properties. * Cons: May be slower due to the need to access each property individually. **Other considerations** * The benchmark does not account for differences in iterator implementations or browser-specific optimizations. MeasureThat.net aims to provide a general idea of performance variations across browsers and versions. * Other factors that might affect performance, such as object size, data type (e.g., primitive vs. reference types), and specific property names, are not considered in this benchmark. **Library: Object.entries()** The `Object.entries()` method is a standardized JavaScript function introduced in ECMAScript 2015 (ES6). It returns an array of key-value pairs for the specified object. This method is used to create iterators that can be iterated over using various loop constructs, including `for...of` and `forEach`. **Special JS feature: Async/await** There are no indications in the provided benchmark code or context that async/await syntax is being used. However, if you were to add an asynchronous function to this benchmark, MeasureThat.net would likely accommodate it. Keep in mind that measuring performance for small scripts like these can be affected by various factors beyond just the script's execution time, such as the test environment, browser version, and available resources.
Related benchmarks:
Map Value Iteration
Object iteration vs Map iteration
Object iteration vs Map iteration new
Object iteration vs Map iteration V2
Object iteration vs Map iteration with deep cloning
Comments
Confirm delete:
Do you really want to delete benchmark?