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()] = makeid();
}
const testArray = [];
Object.values(window.parentObj).forEach(v => {
testArray.push(v);
});
const testArray = [];
Object.keys(window.parentObj).map(k => {
testArray.push(window.parentObj[k]);
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.values | |
Object.keys.map |
Test name | Executions per second |
---|---|
Object.values | 12037.2 Ops/sec |
Object.keys.map | 2822.6 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark is comparing two approaches:
Object.values(window.parentObj)
Object.keys(window.parentObj).map(k => window.parentObj[k])
These two approaches are testing how fast they can iterate over the values or keys of an object, respectively.
Options Compared
The options being compared are:
Object.values
Pros and Cons
Object.values
):Object.keys.map
):map
, which can lead to slower performance.Library Usage
The benchmark uses the window.parentObj
object, which is a global object provided by MeasureThat.net. It appears to be an empty object that is populated with random IDs in the script preparation code. The Object.values
and Object.keys.map
functions are built-in JavaScript methods, so no additional libraries are required.
Special JS Feature/Syntax
There doesn't appear to be any special JavaScript features or syntax used in this benchmark. It's a straightforward comparison of two object iteration approaches.
Other Alternatives
If you wanted to compare other approaches, some alternatives could include:
for...in
or for...of
loops to iterate over the object's properties.Keep in mind that these alternatives would require significant changes to the benchmark code and may not be as relevant to this specific comparison.