Skip to content

Commit a762c6b

Browse files
committed
perf(hooks): avoid redundant allocations and writes in SCU and diffed
1 parent 82ed24e commit a762c6b

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

hooks/src/index.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ options.diffed = vnode => {
8383
c.__hooks._list.some(hookItem => {
8484
if (hookItem._pendingArgs) {
8585
hookItem._args = hookItem._pendingArgs;
86+
hookItem._pendingArgs = undefined;
8687
}
87-
hookItem._pendingArgs = undefined;
8888
});
8989
}
9090
previousComponent = currentComponent = null;
@@ -237,33 +237,27 @@ export function useReducer(reducer, initialState, init) {
237237
function updateHookState(p, s, c) {
238238
if (!hookState._component.__hooks) return true;
239239

240-
const stateHooks = hookState._component.__hooks._list.filter(
241-
x => x._component
242-
);
243-
244-
const allHooksEmpty = stateHooks.every(x => !x._nextValue);
245-
// When we have no updated hooks in the component we invoke the previous SCU or
246-
// traverse the VDOM tree further.
247-
if (allHooksEmpty) {
248-
return prevScu ? prevScu.call(this, p, s, c) : true;
249-
}
250-
251240
// We check whether we have components with a nextValue set that
252241
// have values that aren't equal to one another this pushes
253242
// us to update further down the tree
243+
let updatedHook = false;
254244
let shouldUpdate = hookState._component.props !== p;
255-
stateHooks.some(hookItem => {
245+
hookState._component.__hooks._list.some(hookItem => {
256246
if (hookItem._nextValue) {
247+
updatedHook = true;
257248
const currentValue = hookItem._value[0];
258249
hookItem._value = hookItem._nextValue;
259250
hookItem._nextValue = undefined;
260251
if (currentValue !== hookItem._value[0]) shouldUpdate = true;
261252
}
262253
});
263254

264-
return prevScu
265-
? prevScu.call(this, p, s, c) || shouldUpdate
266-
: shouldUpdate;
255+
if (prevScu) {
256+
const result = prevScu.call(this, p, s, c);
257+
return updatedHook ? result || shouldUpdate : result;
258+
}
259+
260+
return !updatedHook || shouldUpdate;
267261
}
268262

269263
currentComponent.shouldComponentUpdate = updateHookState;

0 commit comments

Comments
 (0)