JSベンチ実測MEASURED, NOT RECALLED

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

    Home / Findings

    `startsWith` is 3.9× faster than a `/^s1/` regex

    For a plain prefix check, startsWith beats a regular expression. Hoisting the regex literal out of the loop still leaves a 3.9× gap.

    Result

    Run`/^s1/` regex`String.startsWith`Ratio
    Run 13.8210.9444.05×
    Run 23.3250.8623.86×
    Run 33.0750.8363.68×

    Median spread: `/^s1/` regex CV 9.1% / `String.startsWith` CV 5.2% / Ratio CV 3.9% (computed from the 3 runs)

    Conditions

    One prefix test per string, across 100,000 strings.

    Where this number does not apply

    Once the pattern gets complex — several alternatives, suffix matches, boundary rules — the regex becomes the better tool.

    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.