Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys vs Object.entries Length
(version: 1)
Comparing performance of:
Object.keys vs Object.entries
Created:
8 months ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } window.parentObj = {}; for (let i = 0; i < 100; i++) { window.parentObj[makeid()] = makeid(); }
Tests:
Object.keys
Object.keys(window.parentObj).map((k) => window.parentObj[k].length > 0);
Object.entries
Object.entries(window.parentObj).map(([k, v]) => v.length > 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys
Object.entries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys
678852.3 Ops/sec
Object.entries
186025.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 8 months ago):
The provided benchmark compares the performance of two different JavaScript methods for extracting information from an object: `Object.keys` and `Object.entries`. Both approaches are commonly used in JavaScript but serve slightly different purposes and have different performance characteristics. ### What Is Tested: 1. **Object.keys**: This method retrieves an array of the object's own enumerable property names (keys). The benchmark case then maps over these keys to check if the length of their corresponding values in `window.parentObj` is greater than zero. - **Test Case**: ```javascript Object.keys(window.parentObj).map((k) => window.parentObj[k].length > 0); ``` 2. **Object.entries**: This method retrieves an array of the object's own enumerable property `[key, value]` pairs. The benchmark checks the length of the values directly in a similar manner. - **Test Case**: ```javascript Object.entries(window.parentObj).map(([k, v]) => v.length > 0); ``` ### Comparison of Options: - **Pros of `Object.keys`**: - Simpler and possibly faster in retrieving just the keys of the object. - It's widely supported and known, making it easier for developers. - **Cons of `Object.keys`**: - Requires an additional lookup to fetch the value associated with each key. - **Pros of `Object.entries`**: - Retrieves both keys and values in one call, which can be more efficient, particularly when you need to use both in subsequent logic. - **Cons of `Object.entries`**: - Slight complexity when handling the output since you're dealing with arrays of key-value pairs. ### Performance Insights: From the benchmark results: - `Object.keys` performed significantly better with **678,852.31 Executions Per Second** compared to **186,025.45 Executions Per Second** for `Object.entries`. This indicates that in this specific scenario, where you're only concerned with the values of `window.parentObj`, `Object.keys` has a clear performance advantage. ### Libraries and Features: No external libraries are used in this benchmark; it solely relies on native JavaScript methods provided by the ECMAScript specification. ### Alternatives: - **For..in Loop**: You could also use a `for..in` loop to iterate over the object's keys. While it allows more control and can sometimes be more efficient when paired with certain operations, it's generally less performant in cases where you need to filter or transform values. - **Object.values**: If the primary interest is only in the values of the object, `Object.values` could serve as a straightforward alternative. However, the benchmark didn't explicitly test `Object.values` as it focuses on the relation between keys and their corresponding values. ### Conclusion: The benchmark effectively highlights the performance differentials between `Object.keys` and `Object.entries`, focusing on how they can be used to manipulate and query object data. For situations where only the keys are needed, `Object.keys` can offer significant performance benefits. However, if both keys and values are essential, `Object.entries` may provide a more concise syntax at the cost of performance. Software engineers should consider the use case when deciding which method to implement.
Related benchmarks:
Object.entries VS Object.keys
Object.entries VS Object.keys.map
Object.values VS Object.keys.map
Object.entries VS Object.keys.map 2
Object.values VS Object.keys.map 2
Object.entries VS Object.keys VS Object.entries with for of
Object.entries VS Object.keys [simple]
Object.entries VS Object.keys for count
Compare Object.keys and Object.entries
Comments
Confirm delete:
Do you really want to delete benchmark?