if(!Object.entries) {
Object.entries = function(obj) {
return Object.keys(obj).reduce(function(arr, key) {
arr.push([key, obj[key]]);
return arr;
}, []);
}
}
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();
}
var newObj = {};
Object.entries(window.parentObj).forEach(([k, v], i) => {
if ((i % 2) === 0) {
newObj[k] = v;
}
});
var newObj = {};
Object.keys(window.parentObj).forEach((k, i) => {
if ((i % 2) === 0) {
newObj[k] = window.parentObj[k];
}
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
Object.keys |
Test name | Executions per second |
---|---|
Object.entries | 88355.5 Ops/sec |
Object.keys | 267510.1 Ops/sec |
I'll break down the provided benchmark definition and test cases, explaining what's being tested, the pros and cons of different approaches, and other considerations.
Benchmark Definition
The benchmark measures the performance of two JavaScript methods: Object.entries
and Object.keys
. The benchmark is designed to be run in Internet Explorer 11 (IE11), which is an older version of the browser that may have performance differences compared to modern browsers like Chrome.
Script Preparation Code
The script preparation code defines a custom implementation of Object.entries
if it's not already available in the environment. This ensures that the benchmark can be run in environments where Object.entries
is not supported, such as older versions of Internet Explorer.
Html Preparation Code
There is no HTML preparation code provided, which means that the benchmark doesn't create any additional DOM elements or initialize any other HTML-related functionality before running the test cases.
Individual Test Cases
The two test cases measure the performance of Object.entries
and Object.keys
, respectively. Both tests create an object (window.parentObj
) with 100 properties, each assigned a random string value using the makeid()
function. The test then iterates over the properties of the object, copying values to a new object only if the index is even.
What's Being Tested
The benchmark measures the performance of two JavaScript methods:
Object.entries
: returns an array of key-value pairs for the given object.Object.keys
: returns an array of keys for the given object.The benchmark tests how fast each method can iterate over the properties of an object and copy values to a new object, with the index being even.
Options Compared
Two options are compared:
Object.entries
: iterates over the key-value pairs of the object using forEach
.Object.keys
: iterates over the keys of the object using forEach
.Pros and Cons of Different Approaches
Object.entries
has a few advantages:Object.keys
, as it doesn't require a separate loop to get the keys.Object.entries
also has some drawbacks:[key, value]
syntax, which might not be available in older browsers like IE11.Object.keys
has some advantages:forEach
with Object.entries
.Object.keys
also has some drawbacks:Object.entries
, which can be slower than accessing them directly.Other Considerations
window.parentObj
object has 100 properties, each assigned a random string value. This means that the benchmark is sensitive to the size of the input data.makeid()
function generates random strings, which can affect the performance of the benchmark depending on the distribution of the generated values.Alternative Approaches
Other alternatives to using Object.entries
and Object.keys
include:
for...in
: iterates over the properties of an object using a traditional loop.Array.from()
: creates an array from the keys of an object, which can then be used with other methods like forEach
.mapKeys()
or mapValues()
, which provide similar functionality to Object.entries
and Object.keys
.