Skip to content

Commit c427a83

Browse files
committed
perf(hooks): avoid redundant allocations and writes in SCU and diffed (#5115)
(cherry picked from commit 6aced21)
1 parent 174e113 commit c427a83

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

hooks/src/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ options.diffed = vnode => {
8686
c.__hooks._list.some(hookItem => {
8787
if (hookItem._pendingArgs) {
8888
hookItem._args = hookItem._pendingArgs;
89+
hookItem._pendingArgs = undefined;
8990
}
90-
hookItem._pendingArgs = undefined;
9191
});
9292
}
9393
previousComponent = currentComponent = null;
@@ -240,16 +240,14 @@ export function useReducer(reducer, initialState, init) {
240240
function updateHookState(p, s, c) {
241241
if (!hookState._component.__hooks) return true;
242242

243-
const hooksList = hookState._component.__hooks._list;
244243
// We check whether we have components with a nextValue set that
245244
// have values that aren't equal to one another this pushes
246245
// us to update further down the tree
247-
let shouldUpdate =
248-
hookState._component.props !== p ||
249-
hooksList.every(x => !x._nextValue);
250-
251-
hooksList.some(hookItem => {
246+
let updatedHook = false;
247+
let shouldUpdate = hookState._component.props !== p;
248+
hookState._component.__hooks._list.some(hookItem => {
252249
if (hookItem._nextValue) {
250+
updatedHook = true;
253251
const currentValue = hookItem._value[0];
254252
hookItem._value = hookItem._nextValue;
255253
hookItem._nextValue = undefined;
@@ -258,9 +256,12 @@ export function useReducer(reducer, initialState, init) {
258256
}
259257
});
260258

261-
return prevScu
262-
? prevScu.call(this, p, s, c) || shouldUpdate
263-
: shouldUpdate;
259+
if (prevScu) {
260+
const result = prevScu.call(this, p, s, c);
261+
return updatedHook ? result || shouldUpdate : result;
262+
}
263+
264+
return !updatedHook || shouldUpdate;
264265
}
265266

266267
currentComponent.shouldComponentUpdate = updateHookState;

0 commit comments

Comments
 (0)