Skip to content

Plain number[] element access ~26x slower than Node in hot loops (community histogram benchmark); representation-selection Phase 4 territory #6904

Description

@proggeramlug

Summary

Community-reported (Discord): an adaptive counting benchmark runs ~25× slower than Node. Reproduced and decomposed. The dominant cost is plain number[] element access (counts[v] read + write in a hot loop); function-call overhead is secondary; arithmetic itself is near-native.

Reproduction (deterministic variant, 5M iterations, byte-identical output vs Node)

Inner loop: const v = random(); counts[v] = (counts[v] || 0) + 1;

Ablation (per-item ns, perry 0.5.1220 vs Node 26.3, same machine/load, ratios stable):

variant Node Perry ratio
pure inline math (no call, no array) 1.6 4.6 ~3×
+ random() as function call 10.5 35.4 3.4×
+ Int32Array counts 10.7 69.4 6.5×
+ plain number[] counts (packed, pre-filled) 9.4 244.7 26×
original shape (holey []) 9.2 263.1 29×

So of the ~214 ns/item on the original script: ~210 ns is the plain-array element read+write, ~31 ns the call, math is fine.

Analysis

Repro files

Deterministic benchmark + ablation harness available on request (seeded LCG replacing Math.random, fixed batch sizes; outputs byte-identical to Node).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions