Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object entry counting: Object.entries VS Object.keys VS Object.values
(version: 0)
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 < 100; i++) { window.parentObj[makeid()] = { innerVal: makeid() }; }
Tests:
Object.entries
const newObj = {}; newObj.count = Object.entries(window.parentObj).length;
Object.keys
const newObj = {}; newObj.count = Object.keys(window.parentObj).length;
Object.values
const newObj = {}; newObj.count = 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 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
566538.1 Ops/sec
Object.keys
4428614.0 Ops/sec
Object.values
1761631.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance of three methods for counting object entries: `Object.entries`, `Object.keys`, and `Object.values`. These methods are part of the ECMAScript standard and provide a way to iterate over an object's properties (key-value pairs). **Test Cases** There are three test cases: 1. **`Object.entries`**: This method returns an array of key-value pairs, where each pair is an array with two elements: the key and the value. 2. **`Object.keys`**: This method returns an array of strings representing the object's property keys. 3. **`Object.values`**: This method returns an array of values (i.e., the second element of each key-value pair) in the order they were added to the object. **Comparison** The benchmark is comparing the performance of these three methods on a large object with 100 properties, generated by creating a random ID for each property and assigning a random value. The test case uses the `window.parentObj` object, which is created by the script preparation code provided in the benchmark definition. **Library and Purpose** * None of the libraries are explicitly used in this benchmark. * The `Object.entries`, `Object.keys`, and `Object.values` methods are part of the ECMAScript standard and are not considered external libraries. **Special JS Features or Syntax** None of the test cases use any special JavaScript features or syntax beyond what is required by the ECMAScript standard. **Pros and Cons** Here's a brief summary of the pros and cons for each method: * **`Object.entries`**: Pros: Returns an array of key-value pairs, which can be useful for iterating over properties. Cons: May incur additional overhead due to the need to create arrays. * **`Object.keys`**: Pros: Returns an array of property keys, which can be faster than `Object.entries`. Cons: Does not return values, so you would need to access them separately. * **`Object.values`**: Pros: Returns an array of values, which can be useful for accessing properties without their keys. Cons: May incur additional overhead due to the need to create arrays. **Alternative Approaches** If you're looking for alternative approaches to count object entries, consider: 1. Using `for...in`: Iterate over the object's property names using a `for` loop. 2. Using `Object.getOwnPropertyNames()`: Returns an array of property names (including non-enumerable properties). 3. Using a custom loop: Write your own loop to iterate over the object's properties. However, keep in mind that these approaches may have different performance characteristics and may not be as efficient as using the built-in `Object.entries`, `Object.keys`, or `Object.values` methods.
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
Comments
Confirm delete:
Do you really want to delete benchmark?