Skip to content
Myr

Skip new references for arrays when sort or reverse is a no-op

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Mark Erikson · committed by Mark Erikson · parent eea464f · Browse files · 2 files+16 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+16 added−0 removed

  • tests:+11 added−0 removed
  • other:+5 added−0 removed

2 files+16 added−0 removed

modified:__tests__/base.js+11 added
340 unmodified lines
341 expect(next).not.toBe(base)342 expect([...next].find(Array.isArray)).toBe(arr)343 })344345 it("sort()/reverse() on arrays with fewer than two elements return the base", () => {346 const base = {empty: [], single: [1]}347 const next = produce(base, d => {348 d.empty.sort()349 d.empty.reverse()350 d.single.sort((a, b) => a - b)351 d.single.reverse()352 })353 expect(next).toBe(base)354 })355356 it("no-op splice() on an array inside a Set preserves the array's identity", () => {357 const arr = [1, 2, 3]358 const base = new Set([arr, {x: 1}])359 const next = produce(base, d => {
3945 unmodified lines
modified:src/plugins/arrayMethods.ts+5 added
299 unmodified lines
300 state: ProxyArrayState,301 method: string,302 args: any[]303 ) {304 // Reordering an array with fewer than two elements cannot change it,305 // so preserve structural sharing and skip copying (see306 // handleSimpleOperation for why this must run before prepareCopy).307 if (latest(state).length <= 1) return state.draft_308309 return executeArrayMethod(310 state,311 () => {312 ;(state.copy_! as any)[method](...args)
196 unmodified lines