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];
}
});
const newObj = {};
let i = 0;
for(const k of Object.keys(window.parentObj)){
if ((i % 2) === 0) {
newObj[k] = window.parentObj[k];
}
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
Object.keys | |
Object.keys with extra array | |
Object.entries without array | |
Object.entries for loop |
Test name | Executions per second |
---|---|
Object.entries | 120431.4 Ops/sec |
Object.keys | 388752.3 Ops/sec |
Object.keys with extra array | 322587.1 Ops/sec |
Object.entries without array | 119813.3 Ops/sec |
Object.entries for loop | 384973.6 Ops/sec |
I'll break down the provided JSON and benchmark preparation code to explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark is designed to compare different approaches for iterating over object keys (values) in JavaScript. The goal is to determine which approach performs better.
Script Preparation Code
This script creates a large object window.parentObj
with 100 properties, each with an inner value. The script also defines a makeid()
function to generate random IDs for the properties.
Html Preparation Code
There is no HTML preparation code provided, which means that the benchmark is likely running in a headless browser or using a command-line interface (CLI) tool.
Individual Test Cases
The test cases compare four different approaches:
Object.keys()
method to get an array of object keys.Object.entries()
method to get an array of key-value pairs.Object.keys()
and creates a new array by extracting the key from the first element of the result.Object.entries()
but attempts to iterate over it as if it were an array.Comparison
The test cases are comparing the performance of each approach in terms of:
Approach Comparison
Here's a brief overview of each approach and their pros and cons:
Object.entries()
as if it were an array, which is incorrect. It will likely cause errors or unexpected behavior.Library Usage
None of the test cases use a library or external dependency.
Special JS Feature or Syntax
There are no special JavaScript features or syntax used in this benchmark. The focus is on comparing different iteration approaches.
Other Alternatives
Some alternative approaches to iterating over object keys include:
for...in
loop, which can be slower than other methods.Array.prototype.forEach()
, which can also introduce overhead due to array creation.for...of
loop and indexing into the object directly.It's worth noting that the best approach will depend on the specific use case and requirements.