Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.2.3] - 2026-09-16

### Changed
- **Batched DOM insertion.** Adding several items at once now renders each item
individually (each needs its own Mustache pass and its own `__jq_$el`) but
performs a single DOM write through a `DocumentFragment`, instead of one
`.after()` per item. Inserting one at a time pays the cost of touching an
attached, already-large container once per item; a fragment pays it once.
Applies to `splice()` with multiple additions, `push(...items)`,
`unshift(...items)`, `__sortedBatchAdd` and `__syncDomOrder` (so `sort()`,
`reverse()` and sorted inserts benefit too). A single-item insert keeps the
previous path, where a fragment would be pure overhead.

Measured in Chrome on rows carrying realistic markup (nested spans, badges,
buttons), one batched push: 1,000 rows 134ms -> 103ms, 2,000 rows 613ms ->
374ms, 4,000 rows 1,274ms -> 1,013ms.

`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.

## [2.2.2] - 2026-08-30

### Fixed
Expand Down
94 changes: 67 additions & 27 deletions dist/js/jq-repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,54 @@
return $render;
}

// Insert a batch of already-rendered elements after `anchor`, in order.
//
// One DOM write, not one per item. Inserting each element separately with
// .after() pays the cost of touching an attached, already-large container
// once per item; a fragment pays it once. Measured in Chrome on a table
// whose rows carry the markup a real app row does (nested spans, badges,
// buttons), comparing a single batched push before and after this change:
// 1,000 rows 134ms -> 103ms, 2,000 rows 613ms -> 374ms, 4,000 rows
// 1,274ms -> 1,013ms. Worth having, and it does not change the shape of
// the curve -- rendering stays ~0.3ms/row, so a caller that rebuilds a
// large list on every change still needs to rebuild less often.
//
// The elements are still rendered individually (each one needs its own
// Mustache pass and its own `__jq_$el`); only the DOM write is batched.
// `__put` is deliberately NOT called from here -- callers invoke it after
// this returns, so the hook still sees an element that is in the document.
__insertAfter(anchor, renderedList) {
if (!renderedList.length) return;
const $anchor = (anchor && anchor.length) ? anchor : this.$this;

// A single element has nothing to batch, and building a fragment for it
// is pure overhead.
if (renderedList.length === 1) {
$anchor.after(renderedList[0]);
return;
}

const anchorEl = $anchor[$anchor.length - 1];
const parent = anchorEl && anchorEl.parentNode;
if (!parent) {
// Detached anchor (the placeholder before the list is in the
// document): nothing to optimise, and insertBefore would throw.
let previous = $anchor;
for (const $el of renderedList) {
previous.after($el);
previous = $el;
}
return;
}

const frag = anchorEl.ownerDocument.createDocumentFragment();
for (const $el of renderedList) {
const nodes = $el.get();
for (let i = 0; i < nodes.length; i++) frag.appendChild(nodes[i]);
}
parent.insertBefore(frag, anchorEl.nextSibling);
}

__refreshIndexAttr(index, item) {
if (!item || !item.__jq_$el) return;
if (this.__jqIndexKey && item.hasOwnProperty(this.__jqIndexKey)) {
Expand Down Expand Up @@ -428,18 +476,14 @@
previousElement = this.$this;
}

const anchor = (previousElement && previousElement.length) ? previousElement : this.$this;
const rendered = [];
for (let i = 0; i < toAdd.length; i++) {
let newItemData = toAdd[i];
let $render = this.__renderItem(index + i, newItemData);

if (previousElement && previousElement.length) {
previousElement.after($render);
} else {
this.$this.after($render);
}
previousElement = $render;

this.__put($render, newItemData, this);
rendered.push(this.__renderItem(index + i, toAdd[i]));
}
this.__insertAfter(anchor, rendered);
for (let i = 0; i < toAdd.length; i++) {
this.__put(rendered[i], toAdd[i], this);
}
}

Expand Down Expand Up @@ -481,7 +525,8 @@
}
}

let previousElement = this.$this;
const ordered = [];
const freshlyRendered = [];
for (let i = 0; i < this.length; i++) {
const item = this[i];
if (!item) continue;
Expand All @@ -490,17 +535,16 @@
if (!$el || !$el.length) {
// New item: render fresh.
$el = this.__renderItem(i, item);
this.__put($el, item, this);
freshlyRendered.push([$el, item]);
}
this.__refreshIndexAttr(i, item);

if (previousElement && previousElement.length) {
previousElement.after($el);
} else {
this.$this.after($el);
}
previousElement = $el;
ordered.push($el);
}
this.__insertAfter(this.$this, ordered);
// After insertion, so `put` sees an element that is in the document
// (it was previously called before the element was inserted here,
// unlike every other call site).
for (const [$el, item] of freshlyRendered) this.__put($el, item, this);
}

pop() {
Expand All @@ -517,19 +561,15 @@
}
}

let previousElement = this.$this;
const ordered = [];
for (let i = 0; i < this.length; i++) {
const item = this[i];
if (item && item.__jq_$el) {
this.__refreshIndexAttr(i, item);
if (previousElement && previousElement.length) {
previousElement.after(item.__jq_$el);
} else {
this.$this.after(item.__jq_$el);
}
previousElement = item.__jq_$el;
ordered.push(item.__jq_$el);
}
}
this.__insertAfter(this.$this, ordered);
return this;
}

Expand Down
Loading
Loading