Skip to content

Commit 803b106

Browse files
authored
Use == where possible and precompute .length (#4599)
1 parent dee5673 commit 803b106

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

src/diff/children.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export function diffChildren(
7373
newParentVNode,
7474
renderResult,
7575
oldChildren,
76-
oldDom
76+
oldDom,
77+
newChildrenLength
7778
);
7879

7980
for (i = 0; i < newChildrenLength; i++) {
@@ -151,7 +152,8 @@ function constructNewChildrenArray(
151152
newParentVNode,
152153
renderResult,
153154
oldChildren,
154-
oldDom
155+
oldDom,
156+
newChildrenLength
155157
) {
156158
/** @type {number} */
157159
let i;
@@ -160,7 +162,6 @@ function constructNewChildrenArray(
160162
/** @type {VNode} */
161163
let oldVNode;
162164

163-
const newChildrenLength = renderResult.length;
164165
let oldChildrenLength = oldChildren.length,
165166
remainingOldChildren = oldChildrenLength;
166167

@@ -177,7 +178,7 @@ function constructNewChildrenArray(
177178
typeof childVNode == 'boolean' ||
178179
typeof childVNode == 'function'
179180
) {
180-
childVNode = newParentVNode._children[i] = null;
181+
newParentVNode._children[i] = null;
181182
continue;
182183
}
183184
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
@@ -258,7 +259,7 @@ function constructNewChildrenArray(
258259
if (typeof childVNode.type != 'function') {
259260
childVNode._flags |= INSERT_VNODE;
260261
}
261-
} else if (matchingIndex !== skewedIndex) {
262+
} else if (matchingIndex != skewedIndex) {
262263
// When we move elements around i.e. [0, 1, 2] --> [1, 0, 2]
263264
// --> we diff 1, we find it at position 1 while our skewed index is 0 and our skew is 0
264265
// we set the skew to 1 as we found an offset.
@@ -301,7 +302,7 @@ function constructNewChildrenArray(
301302
if (remainingOldChildren) {
302303
for (i = 0; i < oldChildrenLength; i++) {
303304
oldVNode = oldChildren[i];
304-
if (oldVNode != null && (oldVNode._flags & MATCHED) === 0) {
305+
if (oldVNode != null && (oldVNode._flags & MATCHED) == 0) {
305306
if (oldVNode._dom == oldDom) {
306307
oldDom = getDomSibling(oldVNode);
307308
}
@@ -347,7 +348,7 @@ function insert(parentVNode, oldDom, parentDom) {
347348

348349
do {
349350
oldDom = oldDom && oldDom.nextSibling;
350-
} while (oldDom != null && oldDom.nodeType === 8);
351+
} while (oldDom != null && oldDom.nodeType == 8);
351352

352353
return oldDom;
353354
}
@@ -402,16 +403,16 @@ function findMatchingIndex(
402403
// If there is an unkeyed functional VNode, that isn't a built-in like our Fragment,
403404
// we should not search as we risk re-using state of an unrelated VNode.
404405
let shouldSearch =
405-
(typeof type !== 'function' || type === Fragment || key) &&
406+
(typeof type != 'function' || type === Fragment || key) &&
406407
remainingOldChildren >
407-
(oldVNode != null && (oldVNode._flags & MATCHED) === 0 ? 1 : 0);
408+
(oldVNode != null && (oldVNode._flags & MATCHED) == 0 ? 1 : 0);
408409

409410
if (
410411
oldVNode === null ||
411412
(oldVNode &&
412413
key == oldVNode.key &&
413414
type === oldVNode.type &&
414-
(oldVNode._flags & MATCHED) === 0)
415+
(oldVNode._flags & MATCHED) == 0)
415416
) {
416417
return skewedIndex;
417418
} else if (shouldSearch) {
@@ -420,7 +421,7 @@ function findMatchingIndex(
420421
oldVNode = oldChildren[x];
421422
if (
422423
oldVNode &&
423-
(oldVNode._flags & MATCHED) === 0 &&
424+
(oldVNode._flags & MATCHED) == 0 &&
424425
key == oldVNode.key &&
425426
type === oldVNode.type
426427
) {
@@ -433,7 +434,7 @@ function findMatchingIndex(
433434
oldVNode = oldChildren[y];
434435
if (
435436
oldVNode &&
436-
(oldVNode._flags & MATCHED) === 0 &&
437+
(oldVNode._flags & MATCHED) == 0 &&
437438
key == oldVNode.key &&
438439
type === oldVNode.type
439440
) {

src/diff/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ export function diff(
171171
c._nextState,
172172
componentContext
173173
) === false) ||
174-
newVNode._original === oldVNode._original)
174+
newVNode._original == oldVNode._original)
175175
) {
176176
// More info about this here: https://gist.github.com/JoviDeCroock/bec5f2ce93544d2e6070ef8e0036e4e8
177-
if (newVNode._original !== oldVNode._original) {
177+
if (newVNode._original != oldVNode._original) {
178178
// When we are dealing with a bail because of sCU we have to update
179179
// the props, state and dirty-state.
180180
// when we are dealing with strict-equality we don't as the child could still
@@ -294,7 +294,7 @@ export function diff(
294294
? MODE_HYDRATE | MODE_SUSPENDED
295295
: MODE_SUSPENDED;
296296

297-
while (oldDom && oldDom.nodeType === 8 && oldDom.nextSibling) {
297+
while (oldDom && oldDom.nodeType == 8 && oldDom.nextSibling) {
298298
oldDom = oldDom.nextSibling;
299299
}
300300

@@ -313,7 +313,7 @@ export function diff(
313313
}
314314
} else if (
315315
excessDomChildren == null &&
316-
newVNode._original === oldVNode._original
316+
newVNode._original == oldVNode._original
317317
) {
318318
newVNode._children = oldVNode._children;
319319
newVNode._dom = oldVNode._dom;
@@ -405,8 +405,8 @@ function diffElementNodes(
405405
let checked;
406406

407407
// Tracks entering and exiting namespaces when descending through the tree.
408-
if (nodeType === 'svg') namespace = SVG_NAMESPACE;
409-
else if (nodeType === 'math') namespace = MATH_NAMESPACE;
408+
if (nodeType == 'svg') namespace = SVG_NAMESPACE;
409+
else if (nodeType == 'math') namespace = MATH_NAMESPACE;
410410
else if (!namespace) namespace = XHTML_NAMESPACE;
411411

412412
if (excessDomChildren != null) {
@@ -418,8 +418,8 @@ function diffElementNodes(
418418
// excessDomChildren so it isn't later removed in diffChildren
419419
if (
420420
value &&
421-
'setAttribute' in value === !!nodeType &&
422-
(nodeType ? value.localName === nodeType : value.nodeType === 3)
421+
'setAttribute' in value == !!nodeType &&
422+
(nodeType ? value.localName == nodeType : value.nodeType == 3)
423423
) {
424424
dom = value;
425425
excessDomChildren[i] = null;
@@ -429,7 +429,7 @@ function diffElementNodes(
429429
}
430430

431431
if (dom == null) {
432-
if (nodeType === null) {
432+
if (nodeType == null) {
433433
return document.createTextNode(newProps);
434434
}
435435

@@ -530,7 +530,7 @@ function diffElementNodes(
530530
newVNode,
531531
oldVNode,
532532
globalContext,
533-
nodeType === 'foreignObject' ? XHTML_NAMESPACE : namespace,
533+
nodeType == 'foreignObject' ? XHTML_NAMESPACE : namespace,
534534
excessDomChildren,
535535
commitQueue,
536536
excessDomChildren
@@ -551,7 +551,7 @@ function diffElementNodes(
551551
// As above, don't diff props during hydration
552552
if (!isHydrating) {
553553
i = 'value';
554-
if (nodeType === 'progress' && inputValue == null) {
554+
if (nodeType == 'progress' && inputValue == null) {
555555
dom.removeAttribute('value');
556556
} else if (
557557
inputValue !== UNDEFINED &&
@@ -560,11 +560,11 @@ function diffElementNodes(
560560
// is missing the progress bar is treated as indeterminate.
561561
// To fix that we'll always update it when it is 0 for progress elements
562562
(inputValue !== dom[i] ||
563-
(nodeType === 'progress' && !inputValue) ||
563+
(nodeType == 'progress' && !inputValue) ||
564564
// This is only for IE 11 to fix <select> value not being updated.
565565
// To avoid a stale select value we need to set the option.value
566566
// again, which triggers IE11 to re-evaluate the select value
567-
(nodeType === 'option' && inputValue !== oldProps[i]))
567+
(nodeType == 'option' && inputValue !== oldProps[i]))
568568
) {
569569
setProperty(dom, i, inputValue, oldProps[i], namespace);
570570
}

src/diff/props.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IS_NON_DIMENSIONAL, SVG_NAMESPACE } from '../constants';
22
import options from '../options';
33

44
function setStyle(style, key, value) {
5-
if (key[0] === '-') {
5+
if (key[0] == '-') {
66
style.setProperty(key, value == null ? '' : value);
77
} else if (value == null) {
88
style[key] = '';
@@ -39,7 +39,7 @@ let eventClock = 0;
3939
export function setProperty(dom, name, value, oldValue, namespace) {
4040
let useCapture;
4141

42-
o: if (name === 'style') {
42+
o: if (name == 'style') {
4343
if (typeof value == 'string') {
4444
dom.style.cssText = value;
4545
} else {
@@ -65,14 +65,14 @@ export function setProperty(dom, name, value, oldValue, namespace) {
6565
}
6666
}
6767
// Benchmark for comparison: https://esbench.com/bench/574c954bdb965b9a00965ac6
68-
else if (name[0] === 'o' && name[1] === 'n') {
69-
useCapture = name !== (name = name.replace(CAPTURE_REGEX, '$1'));
68+
else if (name[0] == 'o' && name[1] == 'n') {
69+
useCapture = name != (name = name.replace(CAPTURE_REGEX, '$1'));
7070

7171
// Infer correct casing for DOM built-in events:
7272
if (
7373
name.toLowerCase() in dom ||
74-
name === 'onFocusOut' ||
75-
name === 'onFocusIn'
74+
name == 'onFocusOut' ||
75+
name == 'onFocusIn'
7676
)
7777
name = name.toLowerCase().slice(2);
7878
else name = name.slice(2);
@@ -136,7 +136,7 @@ export function setProperty(dom, name, value, oldValue, namespace) {
136136

137137
if (typeof value == 'function') {
138138
// never serialize functions as attribute values
139-
} else if (value != null && (value !== false || name[4] === '-')) {
139+
} else if (value != null && (value !== false || name[4] == '-')) {
140140
dom.setAttribute(name, name == 'popover' && value == true ? '' : value);
141141
} else {
142142
dom.removeAttribute(name);

src/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { slice } from './util';
1313
*/
1414
export function render(vnode, parentDom, replaceNode) {
1515
// https://github.com/preactjs/preact/issues/3794
16-
if (parentDom === document) {
16+
if (parentDom == document) {
1717
parentDom = document.documentElement;
1818
}
1919

0 commit comments

Comments
 (0)