Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs object.values for objects 2.0
(version: 0)
Comparing performance of:
for-in vs Object.keys vs Object.values
Created:
one year 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--) { let sum = 0; for (let key in obj) { sum += obj[key].num; } }
Object.keys
for (var i=10000; i > 0; i--) { let sum = 0; for (let key of Object.keys(obj)) { sum += obj[key].num; } }
Object.values
for (var i=10000; i > 0; i--) { let sum = 0; for (let value of Object.values(obj)) { sum += value.num; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Object.values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
265.0 Ops/sec
Object.keys
203.8 Ops/sec
Object.values
691.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided benchmark test compares the performance of three different approaches to iterate over an object's properties in JavaScript: 1. **`for-in`**: This approach uses the `in` operator to iterate over the object's property names. 2. **`Object.keys()`**: This approach uses the `Object.keys()` method to get an array of the object's property names, and then iterates over this array using a `for...of` loop. 3. **`Object.values()`**: This approach uses the `Object.values()` method to get an array of the object's property values, and then iterates over this array using a `for...of` loop. **Options compared** The benchmark compares the performance of these three approaches on large objects with multiple properties. **Pros and Cons of each approach:** * **`for-in`**: This approach is simple and widely supported, but it can be slower than other approaches because it iterates over all property names, including inherited ones. Additionally, it doesn't provide a way to specify the iteration order. * **`Object.keys()`**: This approach provides more control over the iteration order and allows for more efficient memory usage because it only iterates over the own property names of the object, excluding inherited properties. However, it requires an additional method call (`Object.keys()`) which may incur a small overhead. * **`Object.values()`**: Similar to `Object.keys()`, this approach provides more control over the iteration order and allows for efficient memory usage by only iterating over the property values. However, it's only available in modern browsers that support ECMAScript 2019 (ES10). **Library used** None of the benchmark tests use any external libraries. **Special JS features or syntax** None of the test cases use special JavaScript features or syntax. They all use standard JavaScript language and syntax. **Other alternatives** In addition to these three approaches, other alternatives for iterating over an object's properties include: * **`for...in` with `hasOwnProperty()`**: This approach uses the `hasOwnProperty()` method to filter out inherited properties. * **Using a `Map` or `Set` data structure**: These data structures provide efficient iteration over their key-value pairs, but they are not typically used for iterating over objects. **Benchmark preparation code** The benchmark preparation code creates an object with multiple properties (`'a'`, `'b'`, ..., `'g'`) and sets each property to have a value of `1`. The code is generated in the "Script Preparation Code" section of the benchmark definition JSON.
Related benchmarks:
For in vs For of
Object.keys vs Object.values
Object.entries vs Object.keys vs for...in
key in object vs object.key
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?