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.values(window.parentObj).forEach((k, i) => {
if ((i % 2) === 0) {
newObj[k] = window.parentObj[k];
}
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
entries | |
keys | |
values |
Test name | Executions per second |
---|---|
entries | 61798.1 Ops/sec |
keys | 46348.5 Ops/sec |
values | 33684.1 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons, and other considerations.
Benchmark Definition
The benchmark is testing three different approaches to accessing object properties: Object.key
(i.e., window.parentObj[k]
), Object.value
(i.e., window.parentObj[v]
), and Object.entries
(i.e., iterating over the object's key-value pairs).
Script Preparation Code
The script preparation code creates an object window.parentObj
with 100 properties, each containing another object with a random inner property value. This object is used to test the different approaches to accessing its properties.
Html Preparation Code
There is no HTML preparation code provided, which suggests that this benchmark is focused on JavaScript performance and not concerned with DOM manipulation or rendering.
Individual Test Cases
Each test case consists of a single line of JavaScript code that defines a new object newObj
using one of the three approaches:
entries
: Iterates over the object's key-value pairs using Object.entries
, assigning values to newObj
only when the index is even.keys
: Iterates over the object's keys using Object.keys
, assigning values to newObj
only when the index is even.values
: Iterates over the object's values using Object.values
, assigning values to newObj
only when the index is even.Library Used
There is no explicit library used in this benchmark, but it relies on the built-in JavaScript Object
methods: Object.entries
, Object.keys
, and Object.values
.
Special JS Features or Syntax
None of the test cases use any special JavaScript features or syntax beyond what's required to demonstrate the different approaches.
Pros and Cons of Each Approach
Here's a brief summary:
Object.entries
and may not be as expressive.Other Considerations
The benchmark is likely designed to:
Alternatives
If you're interested in exploring similar benchmarks or alternatives, here are some suggestions:
Keep in mind that each benchmark has its strengths and weaknesses, and it's essential to choose the right tool for your specific use case and requirements.