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 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 = {};
for(var key in window.parentObj) {
if ((i % 2) === 0) {
newObj[key] = window.parentObj[key]
}
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries | |
Object.keys | |
Object.keys with extra array | |
Object.entries without array | |
Object for..in |
Test name | Executions per second |
---|---|
Object.entries | 27418.3 Ops/sec |
Object.keys | 37569.8 Ops/sec |
Object.keys with extra array | 23536.2 Ops/sec |
Object.entries without array | 33381.2 Ops/sec |
Object for..in | 237421.3 Ops/sec |
Measuring JavaScript performance is crucial for optimizing code and ensuring efficient execution.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmarking test case named "Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array vs. for...in". The test evaluates the performance of four different approaches to iterate over an object:
Object.entries
Object.keys
Object.keys
with an extra arrayObject.entries
without using an array (i.e., directly iterating over object keys)for...in
)Options Compared
The test compares the performance of these five approaches:
Object.entries
: returns an array of a given object's own enumerable key-value pairsObject.keys
: returns an array of a given object's own enumerable property namesObject.keys
with extra array: uses both Object.keys
and an array to iterate over the properties (not recommended, as it introduces unnecessary complexity)Object.entries
without array: directly iterates over the keys using Object.entries
for...in
): uses a traditional for-each loop to iterate over the object's propertiesPros and Cons
Here are some general pros and cons of each approach:
Object.entries
for large objects.Object.entries
, but avoids using an array, making it more concise.Libraries and Special Features
There are no libraries used in this benchmark. However, note that some JavaScript engines (e.g., V8) use internal optimization techniques to improve performance for certain operations.
Special JS Features or Syntax
This test does not explicitly mention any special features or syntax, but it's worth noting that the use of Object.entries
and other modern JavaScript methods can take advantage of browser-specific optimizations, such as:
Other Alternatives
Some alternative approaches to iterating over objects include:
in
keyword: Using a traditional for loop with the in
keyword: for (var key in obj) { ... }
forEach
with array: Iterating over an object's keys using the forEach
method on an array created from the object's keys.Map
or Set
: Converting an object to a Map
or Set
and iterating over its entries.Keep in mind that each approach has its trade-offs, and the best choice depends on the specific use case and performance requirements.