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 = {};
window.parentObjArr = [];
for (let i = 0; i < 100; i++) {
window.parentObj[makeid()] = makeid();
parentObjArr.push(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 = {};
window.parentObjArr.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 | |
array |
Test name | Executions per second |
---|---|
Object.entries | 44433.6 Ops/sec |
Object.keys | 60525.0 Ops/sec |
Object.keys with extra array | 36430.4 Ops/sec |
Object.entries without array | 45967.6 Ops/sec |
array | 339894.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Overview
The benchmark compares the performance of different approaches to iterate over object keys using Object.keys()
and Object.entries()
. The test cases are designed to create an array of objects with unique IDs, where each object has a single key-value pair. The iteration approach is varied to highlight differences in execution time and efficiency.
Test Cases
Object.entries()
, which returns an array of tuples containing each key-value pair.Object.keys()
.Object.entries()
.forEach()
.Library and Syntax
The benchmark uses JavaScript's built-in Object.keys()
and Object.entries()
methods, which are part of the ECMAScript standard (ECMA-262). No external libraries or dependencies are required for this benchmark.
JavaScript Features
None of the test cases use any special JavaScript features like async/await, generators, or lambda functions. The code is straightforward and simple, making it accessible to a wide range of developers.
Performance Comparison
The benchmark measures execution time per second (ExecutionsPerSecond) for each test case, which indicates how many iterations can be performed within a given timeframe. The results show that:
Object.keys
performs the best, followed closely by Array
, indicating that iterating over object keys directly is relatively efficient.Object.entries without array
and Object.entries with extra array
are slower than the previous two cases but still faster than each other.Object.entries
has a higher execution time compared to Object.keys
, likely due to the additional overhead of creating an array from the iterator.Alternatives
Some alternative approaches to iterating over object keys or arrays could be:
forEach()
, but with additional capabilities like automatic iteration and assignment of value to a variable.iterable-iterator
could offer more control over the iteration process.Keep in mind that the choice of iteration approach depends on the specific requirements of your project, including performance constraints, code readability, and maintainability considerations.