Performance: Array and object reactivity gotchas
This affects how quickly the UI updates when arrays or objects change, impacting interaction responsiveness and visual stability.
let items = [1, 2, 3]; items[0] = 10; items = items; // Reassignment triggers reactivity
let items = [1, 2, 3]; items[0] = 10; // Direct mutation // No reassignment, so UI does not update
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct mutation without reassignment | No DOM update triggered | 0 reflows but stale UI | No paint triggered | [X] Bad |
| Mutation followed by reassignment | Minimal DOM update triggered | 1 reflow per update | Paint triggered for changed nodes | [OK] Good |
| Replacing entire array/object | DOM updates as needed | 1 reflow per update | Paint triggered for changed nodes | [OK] Good |
arr[0] = 10 not update the UI?arr[0] = 10 mutates the array but does not assign a new array to arr, so Svelte does not detect the change.obj = { ...obj, key: 'value' }; uses spread syntax to create a new object and assign it to obj, triggering reactivity. Direct property assignment like obj.key = 'value'; or Object.assign(obj, { key: 'value' }); mutate the object without reassignment, and obj.key = 'value'; obj.update(); calls a non-existent method.let numbers = [1, 2, 3]; numbers.push(4);
{numbers.join(',')} immediately after?push() method changes the array but does not reassign it.numbers is not reassigned, Svelte does not detect the change and the UI remains with the old array.let user = { name: 'Alice', age: 25 };
function birthday() {
user.age += 1;
}birthday() not update the UI showing {user.age}?user.age directly, mutating the object.items reactively in Svelte?items[0] = 'new' does not trigger updates unless followed by reassignment.Pass the test and this pattern moves unknown