Skip to content

Commit 36a6a6b

Browse files
authored
fix: optimize bind:group (#12406)
1 parent bf0d1ef commit 36a6a6b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

.changeset/wicked-bikes-matter.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: optimize `bind:group`

packages/svelte/src/internal/client/dom/elements/bindings/input.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ export function bind_value(input, get_value, update) {
5353
});
5454
}
5555

56+
/** @type {Set<HTMLInputElement[]>} */
57+
const pending = new Set();
58+
5659
/**
57-
* @param {Array<HTMLInputElement>} inputs
60+
* @param {HTMLInputElement[]} inputs
5861
* @param {null | [number]} group_index
5962
* @param {HTMLInputElement} input
6063
* @param {() => unknown} get_value
@@ -127,10 +130,17 @@ export function bind_group(inputs, group_index, input, get_value, update) {
127130
}
128131
});
129132

130-
queue_micro_task(() => {
131-
// necessary to maintain binding group order in all insertion scenarios. TODO optimise
132-
binding_group.sort((a, b) => (a.compareDocumentPosition(b) === 4 ? -1 : 1));
133+
if (!pending.has(binding_group)) {
134+
pending.add(binding_group);
133135

136+
queue_micro_task(() => {
137+
// necessary to maintain binding group order in all insertion scenarios
138+
binding_group.sort((a, b) => (a.compareDocumentPosition(b) === 4 ? -1 : 1));
139+
pending.delete(binding_group);
140+
});
141+
}
142+
143+
queue_micro_task(() => {
134144
if (hydration_mismatch) {
135145
var value;
136146

0 commit comments

Comments
 (0)