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 = new Array();
for (let i = 0; i < 100; i++) {
window.parentObj[makeid()] = { innerVal: 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 = {};
for(const k of window.parentObj) {
const i = window.parentObj[k];
if ((i % 2) === 0) {
newObj[k] = i;
}
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
Object.keys | |
Object.keys with extra array | |
Object.entries without array | |
for .. of |
Test name | Executions per second |
---|---|
Object.entries | 137639.3 Ops/sec |
Object.keys | 398019.8 Ops/sec |
Object.keys with extra array | 363528.3 Ops/sec |
Object.entries without array | 138183.4 Ops/sec |
for .. of | 71408832.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark is comparing the performance of four different methods for iterating over an object:
Object.entries
Object.keys
Object.keys
with an extra array (which doesn't make sense in this context)for .. of
These methods are being compared on a large dataset of objects, where each object has 100 properties with random keys and values.
Method Comparison
forEach
to iterate over this array and construct a new object with only every other pair.forEach
to iterate over this array and construct a new object with only every other key-value pair.Object.keys
is unnecessary and might introduce performance overhead due to the creation of an additional data structure.Performance Comparison
The benchmark results show that:
for .. of
is the fastest method, with approximately 36% more executions per second than the next-fastest method.Object.entries
and Object.keys
have similar performance profiles, with Object.entries
being slightly faster (around 2-3%).Pros and Cons of Each Approach
for .. of
syntax.Library and Special JS Feature
There is no explicit mention of any libraries or special JavaScript features being used in this benchmark. However, it's worth noting that Object.entries
was introduced in ECMAScript 2015 (ES6), which might affect compatibility with older browsers or environments.
Alternatives
If you're looking for alternative methods to iterate over objects, consider the following:
_.forEachObject()
and _ _.pairs()
._.each()
and _ _.reduce()
.for
loops or recursive functions.Keep in mind that each method has its pros and cons, and the choice ultimately depends on your specific use case and performance requirements.