Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs object.values for objects v2
(version: 0)
Comparing performance of:
for-in vs Object.values
Created:
3 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) { console.log(obj[key].id); } }
Object.values
for (var i=10000; i > 0; i--) { const values = Object.values(obj); for (n of values) { console.log(n.id) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in
Object.values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
8.8 Ops/sec
Object.values
7.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares three approaches for iterating over objects in JavaScript: 1. `for-in` 2. `object.keys` (or `Object.keys`) 3. `object.values` (or `Object.values`) These approaches are used to iterate over an object's properties and access their values. The benchmark is designed to measure which approach is the fastest. **Options Compared** The options being compared are: * `for-in`: This method iterates over an object's own enumerable properties using a loop with a counter variable (`i`). It also allows accessing the property name (in this case, `key`) and the value. * `object.keys` / `Object.keys`: This method returns an array-like object containing the names of all own enumerable properties. The benchmark uses `for...of` loop to iterate over this array and access the property values. * `object.values` / `Object.values`: Similar to `object.keys`, but returns an array-like object containing the values of all own enumerable properties. **Pros and Cons** Here's a brief summary of each approach: * **for-in**: + Pros: Can access both property names and values in a single iteration. + Cons: Not as efficient as other methods, especially for large objects, since it iterates over all own enumerable properties. * `object.keys` / `Object.keys`: + Pros: Efficient and lightweight, since it only returns an array of property names. + Cons: Requires an additional step to iterate over the array, which may be slower than using a loop with a counter variable. * `object.values` / `Object.values`: + Pros: Similar efficiency to `object.keys`, but returns an array of values instead of property names. + Cons: May not be as useful if you only need access to property names. **Library and Special JS Features** There are no libraries used in this benchmark. However, it does use the `for...of` loop syntax, which is a feature introduced in ECMAScript 2015 (ES6). **Other Alternatives** Besides the three approaches being compared (`for-in`, `object.keys`, and `object.values`), other alternatives for iterating over objects include: * Using a `forEach` method on an array of property names or values. * Using a `reduce` function to iterate over properties and accumulate results. * Using a custom implementation with a loop and counter variable (like the `for-in` approach). Keep in mind that each approach has its trade-offs, and the best choice depends on the specific use case and performance requirements. **Benchmark Context** The benchmark is likely used to: 1. Compare the performance of different iteration approaches for objects. 2. Provide a baseline for future benchmarks or optimizations related to object iteration. 3. Educate developers about the pros and cons of each approach, helping them choose the most efficient method for their use cases. By understanding the trade-offs and characteristics of each approach, developers can make informed decisions when working with objects in JavaScript.
Related benchmarks:
For in vs For of
Object.entries vs Object.keys vs for...in
for... in VS Object.keys() VS Object.entries()
Object.keys vs Object.entries vs Object.values
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?