Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reading object values in loop: Object.keys vs Object.values
(version: 1)
Comparing performance of:
Object.keys vs Object.values
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = {}; for (let i = 0; i < 1000; i++) { obj['abc_' + i] = i; }
Tests:
Object.keys
const keys = Object.keys(obj); keys.forEach(function(key, value) { console.log(obj[key]); });
Object.values
const values = Object.values(obj); values.forEach(function(key, value) { console.log(value); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys
478.2 Ops/sec
Object.values
481.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark measures the performance difference between two approaches to access values in an object: `Object.keys` and `Object.values`. The test creates an object with 1000 properties, each initialized with a unique value. **Options Compared** Two options are compared: 1. **Object.keys**: Returns an array of strings representing the keys of the object. 2. **Object.values**: Returns an array of values of the object. **Pros and Cons of Each Approach** * **Object.keys**: * Pros: More explicit control over accessing object properties, can be used for iteration with custom key-value pairs. * Cons: Requires additional indexing to access values (`obj[key]`), which can lead to slower performance due to string lookup. * **Object.values**: * Pros: Faster access to values since it avoids string lookup and directly returns the values as an array. However, it only provides direct access to object values without iteration capabilities. * Cons: Less flexible for custom iteration logic. **Library Used (None)** No specific library is used in this benchmark. The test relies solely on built-in JavaScript features. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes explicitly mentioned or utilized in the provided code. **Other Alternatives** If you needed to access object properties, some other alternatives could be considered: * Using a `for...in` loop directly on the object (similar to using `Object.keys` but potentially slower due to string lookup): `for (var key in obj) { console.log(obj[key]); }` * Utilizing modern JavaScript features like `Object.entries()` or `Array.from()`: `Object.entries(obj).forEach(([key, value]) => { console.log(value); });` However, for simple access cases with direct values, using `Object.values` is still the recommended approach due to its performance benefits. **Benchmark Preparation Code Analysis** The provided preparation code creates an object (`obj`) and populates it with 1000 properties. The script then uses a loop to iterate over these properties, assigning each property a unique value between 0 and 999. **Individual Test Cases Analysis** Two test cases are defined: * **Object.keys**: Uses `Object.keys()` to get an array of keys from the object and then iterates through this array using a `forEach` callback function to log the corresponding values. * **Object.values**: Directly uses `Object.values()` to get an array of values from the object, iterating over it with another `forEach` callback function to log each value. These test cases are intended to compare the performance and behavior of these two approaches in a simple scenario.
Related benchmarks:
Object.keys vs Object.values
Object.keys(obj)[0] vs for in
Object.entries vs Object.keys vs for...in
Object.keys().length vs for i in object i++ vs Object.entries().length vs Object.values().length
Comments
Confirm delete:
Do you really want to delete benchmark?