Skip to content
Myr

perf: es5 markChangesRecursively

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

This patch only applies to `produce` calls that have a patch listener. - Removed the `diffKeys` function, which created 2 avoidable arrays. Now, the keys are diffed and processed in one pass. - For objects, the last loop over `proxy` has been removed. - Added fast paths for pre-existing keys when looking for added/removed object keys. - Avoid passing removed array values to `markChangesRecursively`. - Unwrap `state.assigned` as a local variable for fast access.

authored by aleclarson · committed by Alec Larson · parent 241a648 · Browse files · 1 file+31 added−35 removed
This commit

State

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

Diff stats

1 file+31 added−35 removed

  • other:+31 added−35 removed

1 file+31 added−35 removed

modified:src/es5.js+31 added-35 removed
125 unmodified lines
126function markChangesRecursively(object) {127 if (!object || typeof object !== "object") return128 const state = object[PROXY_STATE]129 if (!state) return130 const {proxy, base} = state131 if (Array.isArray(object)) {132 if (hasArrayChanges(state)) {133 markChanged(state)134 state.assigned.length = true135 if (proxy.length < base.length)136 for (let i = proxy.length; i < base.length; i++)137 state.assigned[i] = false138 else139 for (let i = base.length; i < proxy.length; i++)140 state.assigned[i] = true141 each(proxy, (index, child) => {142 if (!state.assigned[index]) markChangesRecursively(child)143 })144 }145 } else {146 const {added, removed} = diffKeys(base, proxy)147 if (added.length > 0 || removed.length > 0) markChanged(state)148 each(added, (_, key) => {149 state.assigned[key] = true130 const {proxy, base, assigned} = state131 if (!Array.isArray(object)) {132 // Look for added keys.133 Object.keys(proxy).forEach(key => {134 // The `undefined` check is a fast path for pre-existing keys.135 if (base[key] === undefined && !has(base, key)) {136 assigned[key] = true137 markChanged(state)138 } else if (!assigned[key]) {139 // Only untouched properties trigger recursion.140 markChangesRecursively(proxy[key])141 }142 })151 each(removed, (_, key) => {152 state.assigned[key] = false143 // Look for removed keys.144 Object.keys(base).forEach(key => {145 // The `undefined` check is a fast path for pre-existing keys.146 if (proxy[key] === undefined && !has(proxy, key)) {147 assigned[key] = false148 markChanged(state)149 }150 })154 each(proxy, (key, child) => {155 if (!state.assigned[key]) markChangesRecursively(child)156 })151 } else if (hasArrayChanges(state)) {152 markChanged(state)153 assigned.length = true154 if (proxy.length < base.length) {155 for (let i = proxy.length; i < base.length; i++) assigned[i] = false156 } else {157 for (let i = base.length; i < proxy.length; i++) assigned[i] = true158 }159 for (let i = 0; i < proxy.length; i++) {160 // Only untouched indices trigger recursion.161 if (assigned[i] === undefined) markChangesRecursively(proxy[i])162 }163 }164}165160function diffKeys(from, to) {161 // TODO: optimize162 const a = Object.keys(from)163 const b = Object.keys(to)164 return {165 added: b.filter(key => a.indexOf(key) === -1),166 removed: a.filter(key => b.indexOf(key) === -1)167 }168}169166function hasObjectChanges(state) {167 const {base, proxy} = state168169 // Search for added keys. Start at the back, because non-numeric keys
79 unmodified lines