8000 further performance tuning for v-for re-render · cdxfish/vue@cba32f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit cba32f4

Browse files
committed
further performance tuning for v-for re-render
1 parent 5c3f0b5 commit cba32f4

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/directives/public/for.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,19 @@ const vFor = {
164164
// from cache)
165165
var removalIndex = 0
166166
var totalRemoved = oldFrags.length - frags.length
167+
// when removing a large number of fragments, watcher removal
168+
// turns out to be a perf bottleneck, so we batch the watcher
169+
// removals into a single filter call!
170+
this.vm._vForRemoving = true
167171
for (i = 0, l = oldFrags.length; i < l; i++) {
168172
frag = oldFrags[i]
169173
if (!frag.reused) {
170174
this.deleteCachedFrag(frag)
171175
this.remove(frag, removalIndex++, totalRemoved, inDocument)
172176
}
173177
}
178+
this.vm._vForRemoving = false
179+
this.vm._watchers = this.vm._watchers.filter(w => w.active)
174180

175181
// Final pass, move/insert new fragments into the
176182
// right place.

src/instance/internal/init.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export default function (Vue) {
5151
this._isDestroyed =
5252
this._isReady =
5353
this._isAttached =
54-
this._isBeingDestroyed = false
54+
this._isBeingDestroyed =
55+
this._vForRemoving = false
5556
this._unlinkFn = null
5657

5758
// context:

src/watcher.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { extend, warn, isArray, isObject, nextTick } from './util/index'
21
import config from './config'
32
import Dep from './observer/dep'
43
import { parseExpression } from './parsers/expression'
54
import { pushWatcher } from './batcher'
5+
import {
6+
extend,
7+
warn,
8+
isArray,
9+
isObject,
10+
nextTick
11+
} from './util/index'
612

713
let uid = 0
814

@@ -299,9 +305,10 @@ Watcher.prototype.depend = function () {
299305
Watcher.prototype.teardown = function () {
300306
if (this.active) {
301307
// remove self from vm's watcher list
302-
// we can skip this if the vm if being destroyed
303-
// which can improve teardown performance.
304-
if (!this.vm._isBeingDestroyed) {
308+
// this is a somewhat expensive operation so we skip it
309+
// if the vm is being destroyed or is performing a v-for
310+
// re-render (the watcher list is then filtered by v-for).
311+
if (!this.vm._isBeingDestroyed && !this.vm._vForRemoving) {
305312
this.vm._watchers.$remove(this)
306313
}
307314
var depIds = Object.keys(this.deps)

0 commit comments

Comments
 (0)
0