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

Apply some performance optimisation #1330

Merged
merged 2 commits into from
Sep 4, 2019
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 packages/driver-dom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ export function afterRender({ container }) {

/**
* Remove all children from node.
* @NOTE: Fast path support in web.
* @NOTE: Optimization at web.
*/
export function removeChildren(node) {
node.innerHTML = '';
node.textContent = '';
}
6 changes: 3 additions & 3 deletions packages/rax/src/hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Host from './vdom/host';
import { scheduleEffect, flushEffect } from './vdom/scheduler';
import { is } from './vdom/shallowEqual';
import { isFunction } from './types';
import {isFunction, isNull} from './types';
import { invokeMinifiedError } from './error';

function getCurrentInstance() {
Expand All @@ -22,7 +22,7 @@ function getCurrentRenderingInstance() {
}

function areInputsEqual(inputs, prevInputs) {
if (prevInputs === null || inputs.length !== prevInputs.length) {
if (isNull(prevInputs) || inputs.length !== prevInputs.length) {
return false;
}

Expand Down Expand Up @@ -198,7 +198,7 @@ export function useMemo(create, inputs) {
hooks[hookID] = [create(), inputs];
} else {
const prevInputs = hooks[hookID][1];
if (inputs === null || !areInputsEqual(inputs, prevInputs)) {
if (isNull(inputs) || !areInputsEqual(inputs, prevInputs)) {
hooks[hookID] = [create(), inputs];
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/rax/src/types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const NULL = null;

export function isNull(obj) {
return obj === NULL;
}

export function isFunction(obj) {
return typeof obj === 'function';
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rax/src/vdom/instantiateComponent.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Host from './host';
import { isString, isNumber, isObject, isArray } from '../types';
import {isString, isNumber, isObject, isArray, isNull} from '../types';
import { invokeMinifiedError } from '../error';

function instantiateComponent(element) {
let instance;

if (element === undefined || element === null || element === false || element === true) {
if (element === undefined || isNull(element) || element === false || element === true) {
instance = new Host.Empty();
} else if (isArray(element)) {
instance = new Host.Fragment(element);
Expand Down
4 changes: 2 additions & 2 deletions packages/rax/src/vdom/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import getElementKeyName from './getElementKeyName';
import Instance from './instance';
import BaseComponent from './base';
import toArray from './toArray';
import { isFunction, isArray } from '../types';
import {isFunction, isArray, isNull} from '../types';

const STYLE = 'style';
const CHILDREN = 'children';
Expand Down Expand Up @@ -345,7 +345,7 @@ class NativeComponent extends BaseComponent {
// `driver.removeChildren` is optional driver protocol.
let shouldRemoveAllChildren = Boolean(
driver.removeChildren
&& nextChildrenElements === null || nextChildrenElements && nextChildrenElements.length === 0
&& isNull(nextChildrenElements) || nextChildrenElements && nextChildrenElements.length === 0
);

// Unmount children that are no longer present.
Expand Down
4 changes: 2 additions & 2 deletions packages/rax/src/vdom/shallowEqual.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from '../types';
import {isNull, isObject} from '../types';

const hasOwnProperty = {}.hasOwnProperty;

Expand Down Expand Up @@ -28,7 +28,7 @@ export default function shallowEqual(objA, objB) {
return true;
}

if (!isObject(objA) || objA === null || !isObject(objB) || objB === null) {
if (!isObject(objA) || isNull(objA) || !isObject(objB) || isNull(objB)) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/rax/src/vdom/shouldUpdateComponent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isArray, isString, isNumber, isObject } from '../types';
import {isArray, isString, isNumber, isObject, isNull} from '../types';

function shouldUpdateComponent(prevElement, nextElement) {
let prevEmpty = prevElement === null;
let nextEmpty = nextElement === null;
let prevEmpty = isNull(prevElement);
let nextEmpty = isNull(nextElement);
if (prevEmpty || nextEmpty) {
return prevEmpty === nextEmpty;
}
Expand Down