Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys Vs Object.entries Performance Test
(version: 0)
Comparing performance of:
Object.entries vs Object.keys
Created:
4 years ago
by:
Guest
Jump to the latest result
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 < 10000; i++) { window.parentObj[makeid()] = makeid(); }
Tests:
Object.entries
Object.entries(window.parentObj)
Object.keys
Object.keys(window.parentObj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
1019.9 Ops/sec
Object.keys
3654.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The benchmark measures the performance difference between two JavaScript methods: 1. `Object.keys(window.parentObj)` 2. `Object.entries(window.parentObj)` These methods are used to iterate over the properties of an object. **Options compared:** There are two options being compared: 1. **`Object.keys()`**: This method returns an array of a given object's own enumerable property names. 2. **`Object.entries()`**: This method returns an array of a given object's key-value pairs as tuples, where the first element of each tuple is the key and the second element is the value. **Pros and Cons:** * **`Object.keys()`**: + Pros: - Generally faster than `Object.entries()`, since it only needs to iterate over the property names, not the entire object. - More widely supported across browsers and Node.js versions. + Cons: - Does not provide access to the property values in a single iteration. - May require additional processing if you need to use the property values later. * **`Object.entries()`**: + Pros: - Provides direct access to both key-value pairs and the keys themselves in a single iteration. - Can be more convenient for some use cases, such as when you need to perform multiple operations on each property-value pair. + Cons: - Generally slower than `Object.keys()`, since it needs to iterate over the entire object. **Library usage:** None of the benchmark code uses any external libraries. It only relies on built-in JavaScript features and DOM elements. **Special JS feature or syntax:** There are no special JavaScript features or syntax used in this benchmark. **Other considerations:** * The benchmark uses a fixed-size object with 10,000 properties, which is a relatively large dataset to measure performance. * The `window.parentObj` variable is used as the source object for both benchmarks. This ensures that the results are not influenced by the size of the object being tested. **Other alternatives:** If you want to write your own benchmark for these methods, here's an example: ```javascript function createBenchmarkSize(size) { const obj = {}; for (let i = 0; i < size; i++) { obj[generateId()] = generateId(); } return obj; } function generateId() { // Your own implementation to generate a unique ID string } const benchmarkSize = createBenchmarkSize(10000); Object.keys(benchmarkSize).forEach((key) => { // Perform some operation on the property value (e.g., logging or calculation) }); ``` This example creates a function `createBenchmarkSize` that generates an object with a specified number of properties. The `generateId` function is not implemented here, as it's already provided in the original benchmark code.
Related benchmarks:
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Object values: Object.entries loop for
Object.key vs Object.value vs Object.entries
Array of key values - Object.entries VS Object.keys
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS for .. of
Object.entries VS Object.keys VS Object.values length check
Comments
Confirm delete:
Do you really want to delete benchmark?