Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[MV] Object.keys() vs Object.values()
(version: 1)
Comparing performance of:
Object.keys() vs Object.values()
Created:
7 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var obj = { 'a': 'keyA', 'b': 'keyB', 'c': 'keyC', 'd': 'keyD', 'e': 'keyE', 'f': 'keyF', 'g': 'keyG', };
Tests:
Object.keys()
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach((key) => console.log(key)); }
Object.values()
for (var i=10000; i > 0; i--) { Object.values(obj).forEach((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:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys()
6.2 Ops/sec
Object.values()
6.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The benchmark presented compares the performance of two JavaScript methods, `Object.keys()` and `Object.values()`, when iterating over an object with predefined keys and values. Both methods serve different purposes, and the benchmark evaluates their execution speed in a controlled environment using a loop that runs 10,000 iterations. ### What is Tested 1. **Object.keys()**: - **Purpose**: This method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would. - **Benchmark Code**: The test iterates over the result of `Object.keys()` for the given object `obj` and logs each key to the console. 2. **Object.values()**: - **Purpose**: This method returns an array of a given object's own enumerable property values, in the same order as the keys returned by `Object.keys()`. - **Benchmark Code**: The test iterates over the result of `Object.values()` and logs each value to the console. ### Performance Results From the benchmark results: - **Object.values()**: 6.339 executions per second - **Object.keys()**: 6.211 executions per second This indicates that `Object.values()` performed slightly faster than `Object.keys()` in this specific case. ### Pros and Cons of Each Approach - **Object.keys()**: - **Pros**: - Useful when you need to access property names directly. - Allows for operations where the names of the properties are required. - **Cons**: - In this test, it showed a lower execution rate compared to `Object.values()`. - **Object.values()**: - **Pros**: - Provides a direct way to access the values, which could be more useful depending on the context of use. - Typically, in scenarios where values are more frequently needed than keys, this method is preferred. - **Cons**: - Not useful when property names are needed for further operations. ### Other Considerations 1. **Overall Performance**: Though one method may be faster than the other in a specific benchmark, the overall performance may be influenced by various factors such as the environment (technical specs, browser optimizations) and intended use. 2. **Readability and Intent**: Select the method based on the intention of your code. If you are more interested in keys for iteration, use `Object.keys()`. If you are focused on values, use `Object.values()`. Maintaining code clarity and intent is essential for maintainability. 3. **Alternative Methods**: - **Object.entries()**: This method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. It is useful for situations where both keys and values are needed simultaneously. 4. **Environment Considerations**: The performance can vary significantly between different browsers and environments. Therefore, it's often a good practice to test benchmarks across various platforms to get a holistic view of performance characteristics. ### Final Remarks In practice, the choice between `Object.keys()` and `Object.values()` should be dictated by the requirements of your application. The benchmark indicates `Object.values()` might execute slightly faster in this scenario, but real-world applications might have different performance characteristics based on their context and usage patterns.
Related benchmarks:
for-in vs object.keys
for-in vs Object.keys()
for-in vs object.keys1
test loops
for-in vs object.keys vs for..of object.keys
Object.entries VS Object.keys
test keys 1234
for-in vs object keys map vs object keys loop
for-in vs object.keys (no hof in Object.keys)
Comments
Confirm delete:
Do you really want to delete benchmark?