Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.keys VS Object.values length check
(version: 0)
JS object size performance comparison
Comparing performance of:
Object.entries vs Object.keys vs Object.values
Created:
one year 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 < 100; i++) { window.parentObj[makeid()] = makeid(); }
Tests:
Object.entries
Object.entries(window.parentObj).length
Object.keys
Object.keys(window.parentObj).length
Object.values
Object.values(window.parentObj).length
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries
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; rv:127.0) Gecko/20100101 Firefox/127.0
Browser/OS:
Firefox 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
413040.1 Ops/sec
Object.keys
1922528.2 Ops/sec
Object.values
1387954.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test aims to compare the performance of three different methods for getting the length of an object's keys: `Object.keys()`, `Object.entries()`, and retrieving the length of the `Array.prototype` directly. **Options being compared:** 1. **`Object.keys()`**: This method returns an array of a given object's own enumerable property names, sorted in ascending order. 2. **`Object.entries()`**: This method returns an array of a given object's own enumerable key-value pairs, where each pair is represented as an array with two elements: the first element is the key and the second element is the value. 3. **Directly retrieving `Array.prototype.length`**: This method simply returns the length property of the `Array` prototype. **Pros and cons of each approach:** 1. **`Object.keys()`**: * Pros: Efficient and straightforward, as it directly accesses the object's keys without any additional processing. * Cons: May require more memory allocation for storing the returned array, which could lead to performance issues in certain scenarios. 2. **`Object.entries()`**: * Pros: More intuitive than `Object.keys()`, as it returns both the key and value pairs in a single step. * Cons: May be slower due to the extra processing required to create the inner arrays. 3. **Directly retrieving `Array.prototype.length`**: * Pros: Generally faster, as it bypasses the object's properties altogether. * Cons: Requires knowledge of the `Array` prototype and may not be suitable for all use cases. **Library usage:** None of the benchmarked methods rely on external libraries. However, the `Object.entries()` method relies on the ECMAScript standard, which is part of the JavaScript language itself. **Special JS feature or syntax:** The benchmark uses a few special features: 1. **`Array.prototype.length`**: This property is part of the `Array` prototype in JavaScript and returns the length of an array. 2. **`Math.floor(Math.random() * possible.length)`**: This expression generates a random index between 0 (inclusive) and `possible.length` (exclusive). It's used to generate random keys for the object. **Other alternatives:** If you need to compare other methods for getting the length of an object's keys, some alternative approaches could be: 1. **Using `Object.getOwnPropertyNames()`**: This method returns all properties (including non-enumerable ones) of a given object. 2. **Using a custom implementation with `for...in` loop**: You can use a `for...in` loop to iterate over the object's properties and count them manually. Keep in mind that these alternatives might have different performance characteristics and may not be suitable for all use cases.
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.keys vs Object.entries vs Object.values for determining size
Comments
Confirm delete:
Do you really want to delete benchmark?