Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.*.forEach vs Object.values :D
(version: 0)
seeing if for in is faster than all of the object.values methods
Comparing performance of:
Object values vs Object keys forEach vs Object entries forEach vs object values forEach vs for in
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = new Object() var keys = (new Array(10000)).fill(0).map((x, i) => { return i + 1 }) keys.forEach((x) => { obj['prop' + x] = x })
Tests:
Object values
for(let value of Object.values(obj)){ console.log(value); }
Object keys forEach
Object.keys(obj).forEach(key => console.log(obj[key]));
Object entries forEach
Object.entries(obj).forEach(([key, value]) => console.log(key,'->',value));
object values forEach
Object.values(obj).forEach(value => console.log(value));
for in
for (var key in obj) { console.log(obj[key]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Object values
Object keys forEach
Object entries forEach
object values forEach
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 break down the provided JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark measures the performance of three different ways to iterate over an object in JavaScript: 1. `for (var key in obj)` (using `in`) 2. `Object.values(obj).forEach(value => console.log(value))` 3. `Object.keys(obj).forEach(key => console.log(obj[key]))` 4. `Object.entries(obj).forEach(([key, value]) => console.log(key, '->', value))` The benchmark compares the performance of these four approaches. **Object Iteration Methods** 1. **`for (var key in obj)`**: This method uses a traditional `for` loop with an `in` statement to iterate over the object's properties. The `key` variable takes on the name of each property, and the value is accessible via `obj[key]`. * Pros: Simple, widely supported, and easy to understand. * Cons: Can be slow for large objects due to the use of a traditional loop. 2. **`Object.values(obj).forEach(value => console.log(value))`**: * This method uses the `Object.values()` method to get an array of an object's property values. The `forEach()` method is then used to iterate over the array and log each value. * Pros: Modern, efficient, and provides a clear way to access only the values. * Cons: May require additional imports (e.g., `const { Object } = require('object-assign');`), and may not be supported in older browsers or environments. 3. **`Object.keys(obj).forEach(key => console.log(obj[key]))`**: * This method uses the `Object.keys()` method to get an array of an object's property names. The `forEach()` method is then used to iterate over the array and log each key-value pair. * Pros: Similar to `for (var key in obj)`, but provides access to both keys and values via `obj[key]`. * Cons: May require additional imports, and may not be supported in older browsers or environments. 4. **`Object.entries(obj).forEach(([key, value]) => console.log(key, '->', value))`**: * This method uses the `Object.entries()` method to get an array of an object's property-value pairs. The `forEach()` method is then used to iterate over the array and log each key-value pair. * Pros: Provides access to both keys and values via array destructuring (`[key, value]`). * Cons: May require additional imports, and may not be supported in older browsers or environments. **Benchmark Results** The benchmark results show that: 1. `Object.values(obj).forEach(value => console.log(value))` is the fastest approach (8.612123489379883 exec/sec) 2. `for (var key in obj)` comes second (8.579272270202637 exec/sec) 3. `Object.keys(obj).forEach(key => console.log(obj[key]))` is slower than both of the above (8.70473575592041 exec/sec) 4. `Object.entries(obj).forEach(([key, value]) => console.log(key, '->', value))` is the slowest approach (7.986688613891602 exec/sec) **Alternative Approaches** Other approaches to iterate over an object in JavaScript include: * Using a `Map` instead of an object (e.g., `const map = new Map();`) * Using `JSON.stringify()` and parsing the output as JSON (not recommended due to performance issues) * Using ES6 `for...of` loop with `Object.entries()` * Using library functions like Lodash's `_.forEachValue()` or Ramda's `map`
Related benchmarks:
For in vs For of
For in vs Object.keys.forEach 10000
For in vs For vs Object.keys.forEach
Array.forEach vs Object.keys().forEach
Comments
Confirm delete:
Do you really want to delete benchmark?