var props = {a:1,b:1,c:1,d:1,e:1,f:1,g:1,h:1,i:1,j:1,k:1,l:1,m:1,n:1,o:1,p:1,q:1,r:1,s:1,t:1,u:1,v:1,w:1,x:1,y:1,z:1};
for (var i=0;i<1000;i++){
var key = i.toString();
props[i]=i;
}
for (const p in props) {
if (Object.prototype.hasOwnProperty.call(props, p)) {
props[p] = 2;
}
}
for (var k=Object.keys(props), l=k.length, i=0; i<l; i++) {
props[k[i]] = 2;
}
const keys = Object.keys(props);
const keys_l = keys.length;
for (var k_i=0;k_i<keys_l;k_i++){
const k=keys[k_i]
props[k] = 2
}
for (const key in Object.keys(props)){
props[key] = 2
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
hasOwnProperty | |
for (var k=Object.keys(props), [...] | |
for (var k_i=0;k_i<keys_l;k_i++ | |
for (const key in Object.keys(props)) |
Test name | Executions per second |
---|---|
hasOwnProperty | 43399.1 Ops/sec |
for (var k=Object.keys(props), [...] | 99700.2 Ops/sec |
for (var k_i=0;k_i<keys_l;k_i++ | 100077.6 Ops/sec |
for (const key in Object.keys(props)) | 293.4 Ops/sec |
The provided benchmark compares different methods of iterating over object properties in JavaScript, specifically focusing on two main approaches: using hasOwnProperty
and Object.keys
. This evaluation is important in understanding how different styles of property access can impact performance in various scenarios.
Using hasOwnProperty
with a for...in
Loop:
for (const p in props) {
if (Object.prototype.hasOwnProperty.call(props, p)) {
props[p] = 2;
}
}
hasOwnProperty
for...in
statement and checks if each property is a direct property of the object using hasOwnProperty
. for...in
loop iterates over all properties that are enumerable, leading to additional overhead due to the need for the hasOwnProperty
check.Using Object.keys
with a Loop:
for (var k = Object.keys(props), l = k.length, i = 0; i < l; i++) {
props[k[i]] = 2;
}
for (var k=Object.keys(props), [...]
Object.keys
to get an array of the object's own property names. It then iterates through that array to access each property.Using Object.keys
with a For Loop Based on Length:
const keys = Object.keys(props);
const keys_l = keys.length;
for (var k_i = 0; k_i < keys_l; k_i++) {
const k = keys[k_i];
props[k] = 2;
}
for (var k_i=0;k_i<keys_l;k_i++
Object.keys
method, this also has the memory overhead of creating an array of keys.Incorrect Syntax Usage of Object.keys in For...in Loop:
for (const key in Object.keys(props)) {
props[key] = 2;
}
for (const key in Object.keys(props))
for...in
loop by iterating over the numeric indexes of the array returned by Object.keys
, rather than the keys themselves.The benchmark's performance results reveal significant differences in execution speed:
Object.keys
with a length caching optimization) showed the best performance at approximately 100K executions per second.Object.keys
without length caching) is also efficient but slightly slower at around 99.7K executions per second.hasOwnProperty
with a for...in
loop showed noticeably lower performance (around 43.3K executions per second).This benchmark illustrates how different patterns for iterating over properties can affect execution speed. Using Object.keys
methods is generally more efficient than using a for...in
loop with hasOwnProperty
, especially when accessing a large number of properties.
Alternatives:
Map
in cases where you require key-value pairs with guaranteed order and better performance for large datasets.forEach
on the result of Object.keys(props)
for better readability, although it might have performance overhead due to function calls.for...of
loops with Object.entries()
for cleaner syntax when you also need the values of the object properties, although it typically involves a similar overhead as Object.keys
.This benchmark serves to inform decisions based on performance considerations regarding object property iteration in JavaScript, facilitating choices between clarity and speed depending on the context.