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() };
}
const newObj = {};
newObj.count = Object.entries(window.parentObj).length;
const newObj = {};
newObj.count = Object.keys(window.parentObj).length;
const newObj = {};
newObj.count = Object.values(window.parentObj).length;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
Object.keys | |
Object.values |
Test name | Executions per second |
---|---|
Object.entries | 188589.3 Ops/sec |
Object.keys | 448811.9 Ops/sec |
Object.values | 840091.8 Ops/sec |
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:
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.Object.keys
: This method returns an array of strings representing the object's property keys.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
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:
for...in
: Iterate over the object's property names using a for
loop.Object.getOwnPropertyNames()
: Returns an array of property names (including non-enumerable 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.