JSベンチ実測MEASURED, NOT RECALLED

    ↑↓ で選択、Enter で開く。Esc で閉じる。

    Home / Findings

    `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 loopRatio
    Run 131.34927.7631.13×
    Run 231.54926.9591.17×
    Run 332.50427.0441.20×

    Median spread: `for…in` CV 1.6% / `Object.keys` + array loop CV 1.3% / Ratio CV 2.6% (computed from the 3 runs)

    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.