Skip to content
New issue

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

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

Already on GitHub? # to your account

perf: created NULL constant #4682

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/clone-element.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assign, slice } from './util';
import { createVNode } from './create-element';
import { UNDEFINED } from './constants';
import { NULL, UNDEFINED } from './constants';

/**
* Clones the given VNode, optionally adding attributes/props and replacing its
Expand Down Expand Up @@ -43,6 +43,6 @@ export function cloneElement(vnode, props, children) {
normalizedProps,
key || vnode.key,
ref || vnode.ref,
null
NULL
);
}
24 changes: 12 additions & 12 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assign } from './util';
import { diff, commitRoot } from './diff/index';
import options from './options';
import { Fragment } from './create-element';
import { MODE_HYDRATE } from './constants';
import { MODE_HYDRATE, NULL } from './constants';

/**
* Base Component class. Provides `setState()` and `forceUpdate()`, which
Expand All @@ -28,7 +28,7 @@ export function BaseComponent(props, context) {
BaseComponent.prototype.setState = function (update, callback) {
// only clone state when copying to nextState the first time.
let s;
if (this._nextState != null && this._nextState !== this.state) {
if (this._nextState != NULL && this._nextState !== this.state) {
s = this._nextState;
} else {
s = this._nextState = assign({}, this.state);
Expand All @@ -45,7 +45,7 @@ BaseComponent.prototype.setState = function (update, callback) {
}

// Skip update if updater function returned null
if (update == null) return;
if (update == NULL) return;

if (this._vnode) {
if (callback) {
Expand Down Expand Up @@ -89,18 +89,18 @@ BaseComponent.prototype.render = Fragment;
* @param {number | null} [childIndex]
*/
export function getDomSibling(vnode, childIndex) {
if (childIndex == null) {
if (childIndex == NULL) {
// Use childIndex==null as a signal to resume the search from the vnode's sibling
return vnode._parent
? getDomSibling(vnode._parent, vnode._index + 1)
: null;
: NULL;
}

let sibling;
for (; childIndex < vnode._children.length; childIndex++) {
sibling = vnode._children[childIndex];

if (sibling != null && sibling._dom != null) {
if (sibling != NULL && sibling._dom != NULL) {
// Since updateParentDomPointers keeps _dom pointer correct,
// we can rely on _dom to tell us if this subtree contains a
// rendered DOM node, and what the first rendered DOM node is
Expand All @@ -113,7 +113,7 @@ export function getDomSibling(vnode, childIndex) {
// Only climb up and search the parent if we aren't searching through a DOM
// VNode (meaning we reached the DOM parent of the original vnode that began
// the search)
return typeof vnode.type == 'function' ? getDomSibling(vnode) : null;
return typeof vnode.type == 'function' ? getDomSibling(vnode) : NULL;
}

/**
Expand All @@ -137,9 +137,9 @@ function renderComponent(component) {
oldVNode,
component._globalContext,
component._parentDom.namespaceURI,
oldVNode._flags & MODE_HYDRATE ? [oldDom] : null,
oldVNode._flags & MODE_HYDRATE ? [oldDom] : NULL,
commitQueue,
oldDom == null ? getDomSibling(oldVNode) : oldDom,
oldDom == NULL ? getDomSibling(oldVNode) : oldDom,
!!(oldVNode._flags & MODE_HYDRATE),
refQueue
);
Expand All @@ -158,11 +158,11 @@ function renderComponent(component) {
* @param {import('./internal').VNode} vnode
*/
function updateParentDomPointers(vnode) {
if ((vnode = vnode._parent) != null && vnode._component != null) {
vnode._dom = vnode._component.base = null;
if ((vnode = vnode._parent) != NULL && vnode._component != NULL) {
vnode._dom = vnode._component.base = NULL;
for (let i = 0; i < vnode._children.length; i++) {
let child = vnode._children[i];
if (child != null && child._dom != null) {
if (child != NULL && child._dom != NULL) {
vnode._dom = vnode._component.base = child._dom;
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
export const XHTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';
export const MATH_NAMESPACE = 'http://www.w3.org/1998/Math/MathML';

export const NULL = null;
export const UNDEFINED = undefined;
export const EMPTY_OBJ = /** @type {any} */ ({});
export const EMPTY_ARR = [];
Expand Down
3 changes: 2 additions & 1 deletion src/create-context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { enqueueRender } from './component';
import { NULL } from './constants';

export let i = 0;

Expand All @@ -13,7 +14,7 @@ export function createContext(defaultValue) {
this.getChildContext = () => ctx;

this.componentWillUnmount = () => {
subs = null;
subs = NULL;
};

this.shouldComponentUpdate = function (_props) {
Expand Down
22 changes: 11 additions & 11 deletions src/create-element.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { slice } from './util';
import options from './options';
import { UNDEFINED } from './constants';
import { NULL, UNDEFINED } from './constants';

let vnodeId = 0;

Expand Down Expand Up @@ -31,15 +31,15 @@ export function createElement(type, props, children) {

// If a Component VNode, check for and apply defaultProps
// Note: type may be undefined in development, must never error here.
if (typeof type == 'function' && type.defaultProps != null) {
if (typeof type == 'function' && type.defaultProps != NULL) {
for (i in type.defaultProps) {
if (normalizedProps[i] === UNDEFINED) {
normalizedProps[i] = type.defaultProps[i];
}
}
}

return createVNode(type, normalizedProps, key, ref, null);
return createVNode(type, normalizedProps, key, ref, NULL);
}

/**
Expand All @@ -63,25 +63,25 @@ export function createVNode(type, props, key, ref, original) {
props,
key,
ref,
_children: null,
_parent: null,
_children: NULL,
_parent: NULL,
_depth: 0,
_dom: null,
_component: null,
_dom: NULL,
_component: NULL,
constructor: UNDEFINED,
_original: original == null ? ++vnodeId : original,
_original: original == NULL ? ++vnodeId : original,
_index: -1,
_flags: 0
};

// Only invoke the vnode hook if this was *not* a direct copy:
if (original == null && options.vnode != null) options.vnode(vnode);
if (original == NULL && options.vnode != NULL) options.vnode(vnode);

return vnode;
}

export function createRef() {
return { current: null };
return { current: NULL };
}

export function Fragment(props) {
Expand All @@ -94,4 +94,4 @@ export function Fragment(props) {
* @returns {vnode is VNode}
*/
export const isValidElement = vnode =>
vnode != null && vnode.constructor == UNDEFINED;
vnode != NULL && vnode.constructor == UNDEFINED;
6 changes: 4 additions & 2 deletions src/diff/catch-error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NULL } from '../constants';

/**
* Find the closest error boundary to a thrown error and call it
* @param {object} error The thrown value
Expand All @@ -20,12 +22,12 @@ export function _catchError(error, vnode, oldVNode, errorInfo) {
try {
ctor = component.constructor;

if (ctor && ctor.getDerivedStateFromError != null) {
if (ctor && ctor.getDerivedStateFromError != NULL) {
component.setState(ctor.getDerivedStateFromError(error));
handled = component._dirty;
}

if (component.componentDidCatch != null) {
if (component.componentDidCatch != NULL) {
component.componentDidCatch(error, errorInfo || {});
handled = component._dirty;
}
Expand Down
45 changes: 23 additions & 22 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
EMPTY_ARR,
INSERT_VNODE,
MATCHED,
UNDEFINED
UNDEFINED,
NULL
} from '../constants';
import { isArray } from '../util';
import { getDomSibling } from '../component';
Expand Down Expand Up @@ -79,7 +80,7 @@ export function diffChildren(

for (i = 0; i < newChildrenLength; i++) {
childVNode = newParentVNode._children[i];
if (childVNode == null) continue;
if (childVNode == NULL) continue;

// At this point, constructNewChildrenArray has assigned _index to be the
// matchingIndex for this VNode's oldVNode (or -1 if there is no oldVNode).
Expand Down Expand Up @@ -110,7 +111,7 @@ export function diffChildren(
newDom = childVNode._dom;
if (childVNode.ref && oldVNode.ref != childVNode.ref) {
if (oldVNode.ref) {
applyRef(oldVNode.ref, null, childVNode);
applyRef(oldVNode.ref, NULL, childVNode);
}
refQueue.push(
childVNode.ref,
Expand All @@ -119,7 +120,7 @@ export function diffChildren(
);
}

if (firstChildDom == null && newDom != null) {
if (firstChildDom == NULL && newDom != NULL) {
firstChildDom = newDom;
}

Expand Down Expand Up @@ -174,11 +175,11 @@ function constructNewChildrenArray(
childVNode = renderResult[i];

if (
childVNode == null ||
childVNode == NULL ||
typeof childVNode == 'boolean' ||
typeof childVNode == 'function'
) {
newParentVNode._children[i] = null;
newParentVNode._children[i] = NULL;
continue;
}
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
Expand All @@ -192,19 +193,19 @@ function constructNewChildrenArray(
childVNode.constructor == String
) {
childVNode = newParentVNode._children[i] = createVNode(
null,
NULL,
childVNode,
null,
null,
null
NULL,
NULL,
NULL
);
} else if (isArray(childVNode)) {
childVNode = newParentVNode._children[i] = createVNode(
Fragment,
{ children: childVNode },
null,
null,
null
NULL,
NULL,
NULL
);
} else if (childVNode.constructor === UNDEFINED && childVNode._depth > 0) {
// VNode is already in use, clone it. This can happen in the following
Expand All @@ -215,7 +216,7 @@ function constructNewChildrenArray(
childVNode.type,
childVNode.props,
childVNode.key,
childVNode.ref ? childVNode.ref : null,
childVNode.ref ? childVNode.ref : NULL,
childVNode._original
);
} else {
Expand All @@ -236,7 +237,7 @@ function constructNewChildrenArray(
remainingOldChildren
));

oldVNode = null;
oldVNode = NULL;
if (matchingIndex !== -1) {
oldVNode = oldChildren[matchingIndex];
remainingOldChildren--;
Expand All @@ -248,7 +249,7 @@ function constructNewChildrenArray(
// Here, we define isMounting for the purposes of the skew diffing
// algorithm. Nodes that are unsuspending are considered mounting and we detect
// this by checking if oldVNode._original === null
const isMounting = oldVNode == null || oldVNode._original === null;
const isMounting = oldVNode == NULL || oldVNode._original === NULL;

if (isMounting) {
if (matchingIndex == -1) {
Expand Down Expand Up @@ -302,7 +303,7 @@ function constructNewChildrenArray(
if (remainingOldChildren) {
for (i = 0; i < oldChildrenLength; i++) {
oldVNode = oldChildren[i];
if (oldVNode != null && (oldVNode._flags & MATCHED) == 0) {
if (oldVNode != NULL && (oldVNode._flags & MATCHED) == 0) {
if (oldVNode._dom == oldDom) {
oldDom = getDomSibling(oldVNode);
}
Expand Down Expand Up @@ -342,13 +343,13 @@ function insert(parentVNode, oldDom, parentDom) {
if (oldDom && parentVNode.type && !parentDom.contains(oldDom)) {
oldDom = getDomSibling(parentVNode);
}
parentDom.insertBefore(parentVNode._dom, oldDom || null);
parentDom.insertBefore(parentVNode._dom, oldDom || NULL);
oldDom = parentVNode._dom;
}

do {
oldDom = oldDom && oldDom.nextSibling;
} while (oldDom != null && oldDom.nodeType == 8);
} while (oldDom != NULL && oldDom.nodeType == 8);

return oldDom;
}
Expand All @@ -361,7 +362,7 @@ function insert(parentVNode, oldDom, parentDom) {
*/
export function toChildArray(children, out) {
out = out || [];
if (children == null || typeof children == 'boolean') {
if (children == NULL || typeof children == 'boolean') {
} else if (isArray(children)) {
children.some(child => {
toChildArray(child, out);
Expand Down Expand Up @@ -403,10 +404,10 @@ function findMatchingIndex(
let shouldSearch =
// (typeof type != 'function' || type === Fragment || key) &&
remainingOldChildren >
(oldVNode != null && (oldVNode._flags & MATCHED) == 0 ? 1 : 0);
(oldVNode != NULL && (oldVNode._flags & MATCHED) == 0 ? 1 : 0);

if (
oldVNode === null ||
oldVNode === NULL ||
(oldVNode &&
key == oldVNode.key &&
type === oldVNode.type &&
Expand Down
Loading