Skip to content

perf: use a Map for tracking chunks#313

Merged
antfu merged 1 commit into
Rich-Harris:masterfrom
43081j:jg/faster-chunks
Jul 23, 2026
Merged

perf: use a Map for tracking chunks#313
antfu merged 1 commit into
Rich-Harris:masterfrom
43081j:jg/faster-chunks

Conversation

@43081j

@43081j 43081j commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Using a map for tracking chunks is faster than a plain object. This is most likely due to v8 thinking we're allocating an array-like object, but then demoting it to a dictionary as we add more keys. That cost of changing allocation will slow it down.

These are @internal properties so it should be fine to change them without a breaking change.

Benchmark results

This branch:

construct x 241,484 ops/sec ±0.31% (102 runs sampled)
append x 20,834,409 ops/sec ±3.14% (70 runs sampled)
indent x 14,808 ops/sec ±1.35% (88 runs sampled)
generateMap (no edit) x 2,971 ops/sec ±1.23% (99 runs sampled)
generateMap (edit) x 3,104 ops/sec ±0.36% (97 runs sampled)
generateDecodedMap (no edit) x 3,263 ops/sec ±2.09% (99 runs sampled)
generateDecodedMap (edit) x 3,353 ops/sec ±0.10% (100 runs sampled)
overwrite x 3.21 ops/sec ±5.65% (12 runs sampled)

Master:

construct x 217,230 ops/sec ±0.51% (97 runs sampled)
append x 20,063,587 ops/sec ±5.06% (68 runs sampled)
indent x 13,950 ops/sec ±1.92% (87 runs sampled)
generateMap (no edit) x 2,994 ops/sec ±1.26% (99 runs sampled)
generateMap (edit) x 3,132 ops/sec ±0.33% (97 runs sampled)
generateDecodedMap (no edit) x 3,328 ops/sec ±0.07% (101 runs sampled)
generateDecodedMap (edit) x 3,357 ops/sec ±0.66% (101 runs sampled)
overwrite x 6.04 ops/sec ±2.08% (20 runs sampled)

its not a huge bump, mostly in construct, but still a bump. i noticed generateMap also speeds up in some cases thanks to this

Using a map for tracking chunks is faster than a plain object. This is
most likely due to v8 thinking we're allocating an array-like object,
but then demoting it to a dictionary as we add more keys. That cost of
changing allocation will slow it down.
@antfu
antfu merged commit 25d7461 into Rich-Harris:master Jul 23, 2026
3 checks passed
@43081j
43081j deleted the jg/faster-chunks branch July 23, 2026 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants