Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys vs Object.entries vs Object.values for determining size
(version: 0)
For measuring the fastest way to measure a length of an object
Comparing performance of:
Object.entries vs Object.keys vs Object.values
Created:
2 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 < 5000; i++) { window.parentObj[makeid()] = makeid(); }
Tests:
Object.entries
Object.entries(window.parentObj)
Object.keys
Object.keys(window.parentObj)
Object.values
Object.values(window.parentObj)
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is tested, compared, and their pros/cons. **What is being tested:** The benchmark measures which method is faster to determine the size of an object in JavaScript: 1. `Object.keys()` 2. `Object.entries()` 3. `Object.values()` These methods are used to iterate over the properties of an object and return them as arrays. **Options compared:** * `Object.keys()`: Returns an array of a given object's own enumerable property names. * `Object.entries()`: Returns an array of a given object's own enumerable key-value pairs. * `Object.values()`: Returns an array of a given object's own enumerable values. **Pros and Cons:** * `Object.keys()`: + Pros: Lightweight, fast, and widely supported. + Cons: May not be suitable for objects with large numbers of properties or property names that are long strings. * `Object.entries()`: + Pros: Returns both keys and values, making it a convenient choice for objects with complex data structures. However, it may have performance implications due to the additional processing required. + Cons: May be slower than `Object.keys()` for large objects or objects with simple property names. * `Object.values()`: + Pros: Returns only the values, which can be beneficial when working with objects that have a large number of keys. However, it may not provide as much information about the object's structure as `Object.entries()`. + Cons: May be slower than `Object.keys()` for small objects or objects with simple property names. **Library and purpose:** There is no external library used in this benchmark. The methods being tested are built-in to JavaScript. **Special JS feature or syntax:** There are no special features or syntax used in this benchmark, other than the use of `Math.floor()` and `Math.random()` for generating random property names. **Considerations:** The benchmark's results may be influenced by factors such as: * The size and complexity of the objects being tested. * The type of data stored in the object (e.g., numbers, strings, booleans). * The performance characteristics of the specific JavaScript engine and browser being used. **Alternative approaches:** Other methods for determining the size of an object might include: * Using `Object.getOwnPropertyNames()` or `Object.getOwnPropertyDescriptors()` to retrieve a list of all properties, including non-enumerable ones. * Using a library like Lodash or Underscore.js to provide additional utility functions for working with objects. However, these alternative approaches may not be as efficient or straightforward as the built-in methods being tested in this benchmark.
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.entries VS Object.keys VS Object.values length check
Comments
Confirm delete:
Do you really want to delete benchmark?