JSベンチ実測MEASURED, NOT RECALLED

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

    Home / Findings

    Sorting numbers is ~1.4× slower than sorting strings

    Numeric sort pays for comparator calls on every comparison, so it is slower than the default string sort. But default order puts '10' before '9', which is useless for numbers.

    Result

    Run100k numbers (with comparator)100k strings (default order)Ratio
    Run 161.28044.9051.36×
    Run 261.76145.0841.37×
    Run 361.50345.0141.37×

    Median spread: 100k numbers (with comparator) CV 0.3% / 100k strings (default order) CV 0.2% / Ratio CV 0.2% (computed from the 3 runs)

    Conditions

    Sorting a 100,000-element array five times (numbers with (a,b)=>a-b, strings with lexicographic default).

    Where this number does not apply

    If lexicographic order is acceptable (fixed-width codes), the default sort is faster. Correct numeric ordering requires the comparator, and this gap is what it costs.

    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.