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();
}
Object.entries(window.parentObj)
Object.keys(window.parentObj)
Object.values(window.parentObj)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
Object.keys | |
Object.values |
Test name | Executions per second |
---|---|
Object.entries | 1797.5 Ops/sec |
Object.keys | 7779.7 Ops/sec |
Object.values | 3061.0 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Overview
The test case, "JUST Object.entries VS Object.keys VS Object.values", compares the performance of three methods: Object.entries()
, Object.keys()
, and Object.values()
on a specific JavaScript object. The benchmark is designed to measure which method is the fastest for iterating over an object's properties.
Benchmark Preparation Code
The preparation code creates a large JavaScript object, window.parentObj
, with 10,000 properties where each property has a random key (a string of 5 characters) and a corresponding value. This object is created using a function called makeid()
, which generates a random string of 5 characters.
Individual Test Cases There are three individual test cases:
Object.entries(window.parentObj)
: This method returns an array of [key, value] pairs from the specified object.Object.keys(window.parentObj)
: This method returns an array of keys (string values) from the specified object.Object.values(window.parentObj)
: This method returns an array of values (values associated with the key-value pairs) from the specified object.Comparison The benchmark compares the performance of these three methods by executing each one a large number of times and measuring how many executions per second are performed on a specific browser configuration (Firefox 120, running on a Mac OS X 10.15 desktop).
Options Compared
Object.entries()
vs Object.keys()
: Both methods iterate over the object's properties, but Object.entries()
returns an array of key-value pairs, while Object.keys()
returns only an array of keys.Object.values()
vs Object.keys()
: This is a comparison between two methods that return different types of data: values and keys.Pros and Cons
Object.entries()
:Object.keys()
:Object.values()
:Library None. This benchmark only uses built-in JavaScript methods.
Special JS Feature/Syntax No special features or syntax are used in this benchmark. It's focused on comparing the performance of three standard JavaScript methods.
Alternatives
for...in
, forEach()
, or reduce()
.