Skip to content

Commit 4ddd8fc

Browse files
authored
test perf of skipping some lifecycle hooks for perf (#4366)
1 parent 8d228d2 commit 4ddd8fc

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/diff/index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export function diff(
6262
try {
6363
let c, isNew, oldProps, oldState, snapshot, clearProcessingException;
6464
let newProps = newVNode.props;
65+
const isClassComponent =
66+
'prototype' in newType && newType.prototype.render;
6567

6668
// Necessary for createContext api. Setting this property will pass
6769
// the context value as `this.context` just for this component.
@@ -79,7 +81,7 @@ export function diff(
7981
clearProcessingException = c._processingException = c._pendingError;
8082
} else {
8183
// Instantiate the new component
82-
if ('prototype' in newType && newType.prototype.render) {
84+
if (isClassComponent) {
8385
// @ts-expect-error The check above verifies that newType is suppose to be constructed
8486
newVNode._component = c = new newType(newProps, componentContext); // eslint-disable-line new-cap
8587
} else {
@@ -103,11 +105,11 @@ export function diff(
103105
}
104106

105107
// Invoke getDerivedStateFromProps
106-
if (c._nextState == null) {
108+
if (isClassComponent && c._nextState == null) {
107109
c._nextState = c.state;
108110
}
109111

110-
if (newType.getDerivedStateFromProps != null) {
112+
if (isClassComponent && newType.getDerivedStateFromProps != null) {
111113
if (c._nextState == c.state) {
112114
c._nextState = assign({}, c._nextState);
113115
}
@@ -125,17 +127,19 @@ export function diff(
125127
// Invoke pre-render lifecycle methods
126128
if (isNew) {
127129
if (
130+
isClassComponent &&
128131
newType.getDerivedStateFromProps == null &&
129132
c.componentWillMount != null
130133
) {
131134
c.componentWillMount();
132135
}
133136

134-
if (c.componentDidMount != null) {
137+
if (isClassComponent && c.componentDidMount != null) {
135138
c._renderCallbacks.push(c.componentDidMount);
136139
}
137140
} else {
138141
if (
142+
isClassComponent &&
139143
newType.getDerivedStateFromProps == null &&
140144
newProps !== oldProps &&
141145
c.componentWillReceiveProps != null
@@ -186,7 +190,7 @@ export function diff(
186190
c.componentWillUpdate(newProps, c._nextState, componentContext);
187191
}
188192

189-
if (c.componentDidUpdate != null) {
193+
if (isClassComponent && c.componentDidUpdate != null) {
190194
c._renderCallbacks.push(() => {
191195
c.componentDidUpdate(oldProps, oldState, snapshot);
192196
});
@@ -200,7 +204,7 @@ export function diff(
200204

201205
let renderHook = options._render,
202206
count = 0;
203-
if ('prototype' in newType && newType.prototype.render) {
207+
if (isClassComponent) {
204208
c.state = c._nextState;
205209
c._dirty = false;
206210

@@ -231,7 +235,7 @@ export function diff(
231235
globalContext = assign(assign({}, globalContext), c.getChildContext());
232236
}
233237

234-
if (!isNew && c.getSnapshotBeforeUpdate != null) {
238+
if (isClassComponent && !isNew && c.getSnapshotBeforeUpdate != null) {
235239
snapshot = c.getSnapshotBeforeUpdate(oldProps, oldState);
236240
}
237241

0 commit comments

Comments
 (0)