Skip to content

.tip

· one of the small things

toSorted when the array is not yours to reorder

.sort() sorts in place, so sorting a prop or a cached response reorders it for everyone else holding that reference, and the bug shows up somewhere unrelated hours later.

const newest = posts.toSorted((a, b) => b.year - a.year);
const patched = rows.with(0, { ...rows[0], seen: true });

toSorted, toReversed, toSpliced and with all return a copy and leave the original alone. Node 20 and up, and every browser shipping today. sort is still fine on an array you built a line ago.

javascript