3 ms·
As far as micro-benchmarks go, this one is particularly egregious. It is likely completely dominated by the array reallocation. So a runtime that - say - choo
by justinsb 11y ago
As far as micro-benchmarks go, this one is particularly egregious. It is likely completely dominated by the array reallocation. So a runtime that - say - chooses to preallocate an array at size 1000000, or to go to 1000000 at first resize would win at this benchmark. More realistically, a runtime that conserves memory by reallocating arrays with a 1.5x factor vs a 2x factor will appear slower.
It's exactly because you don't want to rely on the runtime making the right call for your use-case that you preallocate arrays where it makes a difference (i.e. where you're appending a million items).
And 99% of the time you're doing less than 100 items, and so the array reallocation behavior just doesn't matter.