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();
}
const newObj = {};
Object.entries(window.parentObj).forEach(([k, v], i) => {
if ((i % 2) === 0) {
newObj[k] = v;
}
});
const newObj = {};
Object.keys(window.parentObj).forEach((k, i) => {
if ((i % 2) === 0) {
newObj[k] = window.parentObj[k];
}
});
const newObj = {};
Object.keys(window.parentObj).forEach((k, i) => {
const [extraK, v] = [k, window.parentObj[k]]
if ((i % 2) === 0) {
newObj[extraK] = v;
}
});
const newObj = {};
Object.entries(window.parentObj).forEach((keyAndVal, i) => {
if ((i % 2) === 0) {
newObj[keyAndVal[0]] = keyAndVal[1];
}
});
const newObj = {};
const keys = Object.keys(window.parentObj)
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
if ((i % 2) === 0) {
newObj[k] = window.parentObj[k];
}
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
Object.keys | |
Object.keys with extra array | |
Object.entries without array | |
Object.keys as separate array with for loop |
Test name | Executions per second |
---|---|
Object.entries | 59022.5 Ops/sec |
Object.keys | 69349.0 Ops/sec |
Object.keys with extra array | 42081.6 Ops/sec |
Object.entries without array | 59817.7 Ops/sec |
Object.keys as separate array with for loop | 70137.8 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Overview
The provided benchmark definition compares four different approaches to iterate over an object's key-value pairs:
Object.entries
Object.keys
Object.keys
with an extra arrayObject.entries
without an arrayThese approaches are compared in terms of performance, and the results indicate which method is faster.
Options Compared
The four options being compared are:
Object.entries
: Returns an array-like object containing a key-value pair list for each iteration.Object.keys
or creating an extra array. Also, allows for easy iteration over the key-value pairs using destructuring.Object.keys
: Returns an array-like object containing the keys of the given object.Object.keys
with an extra array: Modifies the original object by creating a new array containing only the keys.Object.entries
due to fewer function calls.Object.entries
without an array: Uses a for...of loop to iterate over the key-value pairs.Object.keys
or creating an extra array. Also, allows for easy iteration over the key-value pairs.Library
None of the provided benchmark definitions use a library. The tests are self-contained and rely solely on built-in JavaScript functionality.
Special JS Feature or Syntax
The benchmark definition uses JavaScript 13's const
init syntax for declaring variables, which is more concise than traditional var
declarations.
Other Considerations
Object.entries
might be a better choice. However, if modifying the original object is acceptable and you want to minimize function calls, Object.keys
with an extra array could be faster.Alternatives
Other alternatives to compare would include:
for...in
instead of for...of
loopsObject.keys()
with a for
loop versus using an array's forEach()
methodKeep in mind that the best approach will depend on the specific use case and requirements. This benchmark serves as a starting point for understanding performance differences between these JavaScript iteration methods.