Skip to content

perf: batch DOM insertion through a DocumentFragment (v2.2.3) - #21

Merged
wmantly merged 1 commit into
masterfrom
release/v2.2.3
Sep 16, 2026
Merged

wmantly merged 1 commit into
masterfrom
release/v2.2.3

Conversation

@wmantly

@wmantly wmantly commented Sep 16, 2026

Copy link
Copy Markdown
Owner

Adding several items at once did one .after() per item, so inserting into an attached, already-large container paid that container's cost once per item. Each item is still rendered individually — it needs its own Mustache pass and its own __jq_$el — but the DOM write is now a single DocumentFragment insert.

Covers splice() with multiple additions (so push(...items) and unshift(...items)), __sortedBatchAdd, and __syncDomOrder — sort(), reverse() and sorted inserts benefit too. A single-item insert keeps the old path, where building a fragment would be pure overhead.

Measured

Chrome, rows carrying realistic markup (nested spans, badges, buttons), one batched push:

rows before after
1,000 134ms 103ms
2,000 613ms 374ms
4,000 1,274ms 1,013ms

Behaviour

put is still called with the element already in the document at every call site. __sortedBatchAdd previously called it before insertion, alone among the call sites; it now matches the rest.

Checks

  • 64 mocha tests pass
  • npm run test:types passes
  • dist/ rebuilt (it is what consumers load via main/browser)

Context

Found while investigating a theta-directory tab reaching 4GB. jq-repeat turned out not to be the cause of that leak — a WeakRef test showed 0 of 500 rows retained after 9 replacement cycles and a forced GC. This is an independent improvement, not the fix for that bug.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TnJ9iEHU6m7kYxD5fXPfts

Adding several items at once did one .after() per item, so inserting into an
attached, already-large container paid that container's cost once per item.
Each item is still rendered individually -- it needs its own Mustache pass and
its own __jq_$el -- but the DOM write is now a single fragment insert.

Covers splice() with multiple additions (and therefore push(...items) and
unshift(...items)), __sortedBatchAdd, and __syncDomOrder, so sort(), reverse()
and sorted inserts benefit too. A single-item insert keeps the old path, where
building a fragment would be pure overhead.

Measured in Chrome on rows with realistic markup, one batched push:
1,000 rows 134ms -> 103ms, 2,000 rows 613ms -> 374ms, 4,000 rows 1,274ms ->
1,013ms.

`put` still sees an element that is in the document at every call site;
__sortedBatchAdd previously called it before insertion, alone among the call
sites, and now matches the rest.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TnJ9iEHU6m7kYxD5fXPfts
@wmantly
wmantly merged commit cd42283 into master Sep 16, 2026
3 checks passed
@wmantly
wmantly deleted the release/v2.2.3 branch September 16, 2026 01:29
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.

1 participant