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 = {};
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];
}
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
Object.keys | |
Object.keys with extra array | |
Object.entries without array |
Test name | Executions per second |
---|---|
Object.entries | 80970.7 Ops/sec |
Object.keys | 243913.7 Ops/sec |
Object.keys with extra array | 215500.0 Ops/sec |
Object.entries without array | 83017.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is comparing four different approaches to iterate over an object's entries in JavaScript:
Object.entries()
Object.keys()
without any modificationsObject.keys()
with an extra array (not explicitly used, but modified for the test)Object.entries()
without using an arrayThe test creates a large object (window.parentObj
) with 100 properties and iterates over it using each of the four approaches. The objective is to measure which approach is the fastest.
Options Compared
Here's a brief description of each option:
Object.keys()
: This method returns an array of a given object's own enumerable property names. In this test, it's used as-is and with an extra array (although not explicitly used).Object.keys() + extra array
: This approach is similar to the previous one but with an unnecessary modification (an extra array).Pros and Cons
Here are some pros and cons of each approach:
Object.entries()
:Object.keys()
:Object.entries()
, as it only needs to access property names.Object.keys() + extra array
:Library
None of the options explicitly use a library. However, window.parentObj
suggests that this benchmark might be related to a web development context, where objects are commonly used.
Special JS Feature/Syntax
There's no explicit mention of any special JavaScript features or syntax being tested in this benchmark. However, it's worth noting that the test uses some advanced concepts, such as:
window.parentObj[makeid()] = { innerVal: makeid() }
)Math.floor(Math.random() * possible.length)
)These features are commonly used in JavaScript development, but might not be immediately apparent to less experienced developers.
Other Alternatives
If you're interested in exploring alternative approaches, here are a few examples:
for...in
: Instead of using Object.keys()
or Object.entries()
, you could use the older for...in
loop to iterate over an object's properties.keys()
function to iterate over an object's properties.Symbol.iterator
API or a library like iterable-iterator
can provide more control and flexibility when iterating over large objects.Keep in mind that these alternatives might not be relevant to this specific benchmark, but they demonstrate the flexibility of JavaScript iteration methods.