Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(core): test perf of skipping some lifecycle hooks for perf #4366

Merged
merged 3 commits into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export function diff(
try {
let c, isNew, oldProps, oldState, snapshot, clearProcessingException;
let newProps = newVNode.props;
const isClassComponent =
'prototype' in newType && newType.prototype.render;

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

// Invoke getDerivedStateFromProps
if (c._nextState == null) {
if (isClassComponent && c._nextState == null) {
c._nextState = c.state;
}

if (newType.getDerivedStateFromProps != null) {
if (isClassComponent && newType.getDerivedStateFromProps != null) {
if (c._nextState == c.state) {
c._nextState = assign({}, c._nextState);
}
Expand All @@ -125,17 +127,19 @@ export function diff(
// Invoke pre-render lifecycle methods
if (isNew) {
if (
isClassComponent &&
newType.getDerivedStateFromProps == null &&
c.componentWillMount != null
) {
c.componentWillMount();
}

if (c.componentDidMount != null) {
if (isClassComponent && c.componentDidMount != null) {
c._renderCallbacks.push(c.componentDidMount);
}
} else {
if (
isClassComponent &&
newType.getDerivedStateFromProps == null &&
newProps !== oldProps &&
c.componentWillReceiveProps != null
Expand Down Expand Up @@ -186,7 +190,7 @@ export function diff(
c.componentWillUpdate(newProps, c._nextState, componentContext);
}

if (c.componentDidUpdate != null) {
if (isClassComponent && c.componentDidUpdate != null) {
c._renderCallbacks.push(() => {
c.componentDidUpdate(oldProps, oldState, snapshot);
});
Expand All @@ -200,7 +204,7 @@ export function diff(

let renderHook = options._render,
count = 0;
if ('prototype' in newType && newType.prototype.render) {
if (isClassComponent) {
c.state = c._nextState;
c._dirty = false;

Expand Down Expand Up @@ -231,7 +235,7 @@ export function diff(
globalContext = assign(assign({}, globalContext), c.getChildContext());
}

if (!isNew && c.getSnapshotBeforeUpdate != null) {
if (isClassComponent && !isNew && c.getSnapshotBeforeUpdate != null) {
snapshot = c.getSnapshotBeforeUpdate(oldProps, oldState);
}

Expand Down