`for…in` is 17% slower than `Object.keys`
To enumerate keys, Object.keys is faster. for…in also walks the prototype chain, and that costs extra.
Result
| Run | `for…in` | `Object.keys` + array loop | Ratio |
|---|---|---|---|
| Run 1 | 31.349 | 27.763 | 1.13× |
| Run 2 | 31.549 | 26.959 | 1.17× |
| Run 3 | 32.504 | 27.044 | 1.20× |
Conditions
Enumerating an object with 100,000 keys.
Where this number does not apply
17% is small enough to disappear behind real work. Choosing for…in for readability is often the right call.
Reproduce
cd bench
node --expose-gc micro-fixed.mjs --out ../data/run.json --samples 21 Run it three times on the same machine; if the spread of median stays under 10%, you should land on the same conclusion. If your numbers disagree, tell us and we will retract.