Skip to content

Commit 6de1b9b

Browse files
committed
optimize patch conditionals
1 parent 0bb529a commit 6de1b9b

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

src/core/vdom/patch.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ export const emptyNode = new VNode('', {}, [])
2222

2323
const hooks = ['create', 'activate', 'update', 'remove', 'destroy']
2424

25-
function isUndef (s) {
26-
return s == null
25+
function isUndef (v) {
26+
return v === undefined || v === null
2727
}
2828

29-
function isDef (s) {
30-
return s != null
29+
function isDef (v) {
30+
return v !== undefined && v !== null
31+
}
32+
33+
function isTrue (v) {
34+
return v === true
3135
}
3236

3337
function sameVnode (vnode1, vnode2) {
@@ -58,7 +62,9 @@ export function createPatchFunction (backend) {
5862
for (i = 0; i < hooks.length; ++i) {
5963
cbs[hooks[i]] = []
6064
for (j = 0; j < modules.length; ++j) {
61-
if (modules[j][hooks[i]] !== undefined) cbs[hooks[i]].push(modules[j][hooks[i]])
65+
if (isDef(modules[j][hooks[i]])) {
66+
cbs[hooks[i]].push(modules[j][hooks[i]])
67+
}
6268
}
6369
}
6470

@@ -79,7 +85,7 @@ export function createPatchFunction (backend) {
7985
function removeNode (el) {
8086
const parent = nodeOps.parentNode(el)
8187
// element may have already been removed due to v-html / v-text
82-
if (parent) {
88+
if (isDef(parent)) {
8389
nodeOps.removeChild(parent, el)
8490
}
8591
}
@@ -123,7 +129,7 @@ export function createPatchFunction (backend) {
123129
// in Weex, the default insertion order is parent-first.
124130
// List items can be optimized to use children-first insertion
125131
// with append="tree".
126-
const appendAsTree = data && data.appendAsTree
132+
const appendAsTree = isDef(data) && isTrue(data.appendAsTree)
127133
if (!appendAsTree) {
128134
if (isDef(data)) {
129135
invokeCreateHooks(vnode, insertedVnodeQueue)
@@ -148,7 +154,7 @@ export function createPatchFunction (backend) {
148154
if (process.env.NODE_ENV !== 'production' && data && data.pre) {
149155
inPre--
150156
}
151-
} else if (vnode.isComment) {
157+
} else if (isTrue(vnode.isComment)) {
152158
vnode.elm = nodeOps.createComment(vnode.text)
153159
insert(parentElm, vnode.elm, refElm)
154160
} else {
@@ -170,7 +176,7 @@ export function createPatchFunction (backend) {
170176
// in that case we can just return the element and be done.
171177
if (isDef(vnode.componentInstance)) {
172178
initComponent(vnode, insertedVnodeQueue)
173-
if (isReactivated) {
179+
if (isTrue(isReactivated)) {
174180
reactivateComponent(vnode, insertedVnodeQueue, parentElm, refElm)
175181
}
176182
return true
@@ -179,7 +185,7 @@ export function createPatchFunction (backend) {
179185
}
180186

181187
function initComponent (vnode, insertedVnodeQueue) {
182-
if (vnode.data.pendingInsert) {
188+
if (isDef(vnode.data.pendingInsert)) {
183189
insertedVnodeQueue.push.apply(insertedVnodeQueue, vnode.data.pendingInsert)
184190
}
185191
vnode.elm = vnode.componentInstance.$el
@@ -218,8 +224,8 @@ export function createPatchFunction (backend) {
218224
}
219225

220226
function insert (parent, elm, ref) {
221-
if (parent) {
222-
if (ref) {
227+
if (isDef(parent)) {
228+
if (isDef(ref)) {
223229
nodeOps.insertBefore(parent, elm, ref)
224230
} else {
225231
nodeOps.appendChild(parent, elm)
@@ -250,8 +256,8 @@ export function createPatchFunction (backend) {
250256
}
251257
i = vnode.data.hook // Reuse variable
252258
if (isDef(i)) {
253-
if (i.create) i.create(emptyNode, vnode)
254-
if (i.insert) insertedVnodeQueue.push(vnode)
259+
if (isDef(i.create)) i.create(emptyNode, vnode)
260+
if (isDef(i.insert)) insertedVnodeQueue.push(vnode)
255261
}
256262
}
257263

@@ -310,15 +316,15 @@ export function createPatchFunction (backend) {
310316
}
311317

312318
function removeAndInvokeRemoveHook (vnode, rm) {
313-
if (rm || isDef(vnode.data)) {
319+
if (isDef(rm) || isDef(vnode.data)) {
314320
const listeners = cbs.remove.length + 1
315-
if (!rm) {
316-
// directly removing
317-
rm = createRmCb(vnode.elm, listeners)
318-
} else {
321+
if (isDef(rm)) {
319322
// we have a recursively passed down rm callback
320323
// increase the listeners count
321324
rm.listeners += listeners
325+
} else {
326+
// directly removing
327+
rm = createRmCb(vnode.elm, listeners)
322328
}
323329
// recursively invoke hooks on child component root node
324330
if (isDef(i = vnode.componentInstance) && isDef(i = i._vnode) && isDef(i.data)) {
@@ -420,24 +426,23 @@ export function createPatchFunction (backend) {
420426
// note we only do this if the vnode is cloned -
421427
// if the new node is not cloned it means the render functions have been
422428
// reset by the hot-reload-api and we need to do a proper re-render.
423-
if (vnode.isStatic &&
424-
oldVnode.isStatic &&
429+
if (isTrue(vnode.isStatic) &&
430+
isTrue(oldVnode.isStatic) &&
425431
vnode.key === oldVnode.key &&
426-
(vnode.isCloned || vnode.isOnce)) {
432+
(isTrue(vnode.isCloned) || isTrue(vnode.isOnce))) {
427433
vnode.elm = oldVnode.elm
428434
vnode.componentInstance = oldVnode.componentInstance
429435
return
430436
}
431437
let i
432438
const data = vnode.data
433-
const hasData = isDef(data)
434-
if (hasData && isDef(i = data.hook) && isDef(i = i.prepatch)) {
439+
if (isDef(data) && isDef(i = data.hook) && isDef(i = i.prepatch)) {
435440
i(oldVnode, vnode)
436441
}
437442
const elm = vnode.elm = oldVnode.elm
438443
const oldCh = oldVnode.children
439444
const ch = vnode.children
440-
if (hasData && isPatchable(vnode)) {
445+
if (isDef(data) && isPatchable(vnode)) {
441446
for (i = 0; i < cbs.update.length; ++i) cbs.update[i](oldVnode, vnode)
442447
if (isDef(i = data.hook) && isDef(i = i.update)) i(oldVnode, vnode)
443448
}
@@ -455,15 +460,15 @@ export function createPatchFunction (backend) {
455460
} else if (oldVnode.text !== vnode.text) {
456461
nodeOps.setTextContent(elm, vnode.text)
457462
}
458-
if (hasData) {
463+
if (isDef(data)) {
459464
if (isDef(i = data.hook) && isDef(i = i.postpatch)) i(oldVnode, vnode)
460465
}
461466
}
462467

463468
function invokeInsertHook (vnode, queue, initial) {
464469
// delay insert hooks for component root nodes, invoke them after the
465470
// element is really inserted
466-
if (initial && vnode.parent) {
471+
if (isTrue(initial) && isDef(vnode.parent)) {
467472
vnode.parent.data.pendingInsert = queue
468473
} else {
469474
for (let i = 0; i < queue.length; ++i) {
@@ -538,7 +543,7 @@ export function createPatchFunction (backend) {
538543
}
539544

540545
function assertNodeMatch (node, vnode) {
541-
if (vnode.tag) {
546+
if (isDef(vnode.tag)) {
542547
return (
543548
vnode.tag.indexOf('vue-component') === 0 ||
544549
vnode.tag.toLowerCase() === (node.tagName && node.tagName.toLowerCase())
@@ -549,15 +554,15 @@ export function createPatchFunction (backend) {
549554
}
550555

551556
return function patch (oldVnode, vnode, hydrating, removeOnly, parentElm, refElm) {
552-
if (!vnode) {
553-
if (oldVnode) invokeDestroyHook(oldVnode)
557+
if (isUndef(vnode)) {
558+
if (isDef(oldVnode)) invokeDestroyHook(oldVnode)
554559
return
555560
}
556561

557562
let isInitialPatch = false
558563
const insertedVnodeQueue = []
559564

560-
if (!oldVnode) {
565+
if (isUndef(oldVnode)) {
561566
// empty mount (likely as component), create new root element
562567
isInitialPatch = true
563568
createElm(vnode, insertedVnodeQueue, parentElm, refElm)
@@ -575,7 +580,7 @@ export function createPatchFunction (backend) {
575580
oldVnode.removeAttribute('server-rendered')
576581
hydrating = true
577582
}
578-
if (hydrating) {
583+
if (isTrue(hydrating)) {
579584
if (hydrate(oldVnode, vnode, insertedVnodeQueue)) {
580585
invokeInsertHook(vnode, insertedVnodeQueue, true)
581586
return oldVnode
@@ -606,7 +611,7 @@ export function createPatchFunction (backend) {
606611
nodeOps.nextSibling(oldElm)
607612
)
608613

609-
if (vnode.parent) {
614+
if (isDef(vnode.parent)) {
610615
// component root element replaced.
611616
// update parent placeholder node element, recursively
612617
let ancestor = vnode.parent
@@ -621,7 +626,7 @@ export function createPatchFunction (backend) {
621626
}
622627
}
623628

624-
if (parentElm !== null) {
629+
if (isDef(parentElm)) {
625630
removeVnodes(parentElm, [oldVnode], 0, 0)
626631
} else if (isDef(oldVnode.tag)) {
627632
invokeDestroyHook(oldVnode)

0 commit comments

Comments
 (0)