Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.values vs object.entries for objects
(version: 0)
Comparing performance of:
for-in vs Object.values vs Object.entries
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': { id: 'a', num: 1 }, 'b': { id: 'b', num: 1 }, 'c': { id: 'c', num: 1 }, 'd': { id: 'd', num: 1 }, 'e': { id: 'e', num: 1 }, 'f': { id: 'f', num: 1 }, 'g': { id: 'g', num: 1 }, };
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in obj) { if (obj.hasOwnProperty(key)) { console.log(obj[key].id); } } }
Object.values
for (var i=10000; i > 0; i--) { for (let value of Object.values(obj)) { console.log(value.id); } }
Object.entries
for (var i=10000; i > 0; i--) { for (let [key, value] of Object.entries(obj)) { console.log(value.id); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.values
Object.entries
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 benchmark definition and explain what is tested, compared, pros and cons of different approaches, and other considerations. **Benchmark Definition:** The benchmark defines a test case that measures the performance difference between three different ways to iterate over an object: 1. `for-in`: Uses the `in` operator to iterate over the object's properties. 2. `Object.values`: Uses the `values()` method of the `Object` prototype to get an array of the object's property values. 3. `Object.entries`: Uses the `entries()` method of the `Object` prototype to get an array of the object's key-value pairs. **Benchmark Preparation Code:** The preparation code creates a large object (`obj`) with 10 properties, each containing an `id` and a `num` property. ```javascript var obj = { 'a': { id: 'a', num: 1 }, 'b': { id: 'b', num: 1 }, 'c': { id: 'c', num: 1 }, 'd': { id: 'd', num: 1 }, 'e': { id: 'e', num: 1 }, 'f': { id: 'f', num: 1 }, 'g': { id: 'g', num: 1 }, 'h': { id: 'h', num: 1 }, 'i': { id: 'i', num: 1 }, 'j': { id: 'j', num: 1 } }; ``` **Individual Test Cases:** There are three test cases, each with a different benchmark definition: 1. `for-in`: Uses the `in` operator to iterate over the object's properties. ```javascript for (var key in obj) { if (obj.hasOwnProperty(key)) { console.log(obj[key].id); } } ``` 2. `Object.values`: Uses the `values()` method of the `Object` prototype to get an array of the object's property values. ```javascript for (let value of Object.values(obj)) { console.log(value.id); } ``` 3. `Object.entries`: Uses the `entries()` method of the `Object` prototype to get an array of the object's key-value pairs. ```javascript for (let [key, value] of Object.entries(obj)) { console.log(value.id); } ``` **Performance Comparison:** The benchmark compares the performance of each test case using a JavaScript interpreter. The results show that: * `Object.values` is slightly faster than `for-in`. * `Object.entries` is slower than both `Object.values` and `for-in`. **Pros and Cons:** * `for-in`: This approach can be slower due to the additional checks for the property's existence. However, it can also provide better performance for certain use cases, such as when the object has a large number of properties. * `Object.values`: This approach is generally faster than `for-in` because it avoids the extra checks. However, it may not be suitable for objects with many properties due to memory usage concerns. * `Object.entries`: This approach provides good performance but can be slower than `Object.values` due to the additional overhead of creating an array of key-value pairs. **Other Considerations:** * Memory Usage: The `Object.entries` approach creates a new array of key-value pairs, which can consume more memory. In contrast, `for-in` and `Object.values` do not create additional data structures. * Code Readability: The `for-in` approach is generally considered less readable than the other two options due to the need for explicit checks. **Alternatives:** Other alternatives to these approaches include: * Using `Map` objects instead of regular objects, which provide faster iteration and key-value access. * Using libraries like Lodash or Ramda that provide optimized functions for iterating over arrays and objects. * Using specialized JavaScript engines or compilers that can optimize the code generation process.
Related benchmarks:
For in vs For of
Object.entries vs Object.keys vs for...in
for-in vs object.keys vs object.values for objects test
for-in vs object.keys vs object.values for objects - output object values
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?