Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare Object.keys and Object.entries
(version: 1)
Comparing performance of:
Object.keys vs Object.entries
Created:
one year ago
by:
Guest
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
const newObj = {}; const keys = Object.keys(window.parentObj); for (const k of keys) { newObj[k] = window.parentObj[k]; }
Object.entries
const newObj = {}; const entries = Object.entries(window.parentObj); for (const [k, v] of entries) { newObj[k] = v; }
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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys
207495.8 Ops/sec
Object.entries
89582.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares the performance of two methods for iterating over the properties of an object in JavaScript: `Object.keys` and `Object.entries`. ### Comparison of Options 1. **Object.keys**: - **Functionality**: Returns an array of the property names (keys) of the given object. - **Benchmark Code**: ```javascript const keys = Object.keys(window.parentObj); for (const k of keys) { newObj[k] = window.parentObj[k]; } ``` - **Performance**: In the benchmark results, `Object.keys` performed significantly better with approximately 207,496 executions per second. 2. **Object.entries**: - **Functionality**: Returns an array of a given object's own enumerable string-keyed property [key, value] pairs. - **Benchmark Code**: ```javascript const entries = Object.entries(window.parentObj); for (const [k, v] of entries) { newObj[k] = v; } ``` - **Performance**: The `Object.entries` method had a lower performance, at about 89,582 executions per second. ### Pros and Cons - **Object.keys**: - **Pros**: - Faster performance in this benchmark. - Simpler use case when only needing keys. - **Cons**: - Requires two lookups (one for the key and one for its value) when processing values. - **Object.entries**: - **Pros**: - Provides both keys and values in a single operation, simplifying the code when both are required. - **Cons**: - Lower performance compared to `Object.keys` in this benchmark scenario, which could be a concern for performance-critical applications. ### Considerations 1. **Use Cases**: - Use `Object.keys` when you only need to iterate over the keys of an object. - Utilize `Object.entries` when both keys and values are necessary for the operation. 2. **Performance Context**: - Performance can vary significantly based on the size of the object and the JavaScript engine of the browser. While this benchmark suggests `Object.keys` is faster, performance should always be tested in the context of the actual application. 3. **Browser Variability**: - Benchmark results may differ across various browsers and platforms. This particular benchmark was run on Chrome 135 on a Linux desktop. ### Alternatives 1. **For...in Loop**: The traditional way to iterate over object properties; however, it includes inherited properties which may not be desired and can have performance implications. - Example: ```javascript for (let key in window.parentObj) { if (window.parentObj.hasOwnProperty(key)) { newObj[key] = window.parentObj[key]; } } ``` 2. **Map Object**: If key-value pairs are central to your use case, consider using `Map`, which maintains insertion order and works well with frequent additions/removals. 3. **Array.prototype.forEach**: If the object is first converted into an array (using `Object.keys` or `Object.entries`), you can utilize this method for iteration, which may or may not be more performant based on the scenario. In conclusion, when deciding between `Object.keys` and `Object.entries`, weigh the specific needs of your application and consider the performance benchmarks alongside readability and code maintainability.
Related benchmarks:
Object.entries VS Object.keys
Object.entries 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 with array
Compare Object.keys and Object.entries with array v2
Comments
Confirm delete:
Do you really want to delete benchmark?