ARTICLE AD BOX
I'm benchmarking (in Chrome) two ways of counting the number of properties in a JavaScript object.
I'm using Chrome Version 146.0.7680.31 (Official Build) beta (64-bit).
The first is by looping through each property with a for...in loop and incrementing a counter, and the second is by calling Object.keys(obj).length.
I created a JSFiddle to test each technique, and in Chrome, on the first run, the for...in loop is extremely fast compared to the Object.keys(obj).length technique.
However, with subsequent runs, the for...in loop is much slower.
Why is that so?
First Run With 100,000,000 Iterations (Chrome)
for...in - 0.531 seconds
Object.keys(obj).length - 0.955 seconds
Second Run With 100,000,000 Iterations (Chrome)
for...in - 3.361 seconds
Object.keys(obj).length - 0.994 seconds
JSFiddle
Here's the JSFiddle: https://jsfiddle.net/79bpf2rs

