Skip to content

Commit 6bf49a0

Browse files
committed
fix array filtering perf regression (revert 30bd8be)
1 parent 6a28ce8 commit 6bf49a0

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

src/filters/array-filters.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,17 @@ exports.orderBy = function (arr, sortKey, reverse) {
7373
*/
7474

7575
function contains (val, search) {
76+
var i
7677
if (_.isPlainObject(val)) {
77-
for (var key in val) {
78-
if (contains(val[key], search)) {
78+
var keys = Object.keys(val)
79+
i = keys.length
80+
while (i--) {
81+
if (contains(val[keys[i]], search)) {
7982
return true
8083
}
8184
}
8285
} else if (_.isArray(val)) {
83-
var i = val.length
86+
i = val.length
8487
while (i--) {
8588
if (contains(val[i], search)) {
8689
return true

src/observer/dep.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var _ = require('../util')
2+
var uid = 0
23

34
/**
45
* A dep is an observable that can have multiple
@@ -8,6 +9,7 @@ var _ = require('../util')
89
*/
910

1011
function Dep () {
12+
this.id = uid++
1113
this.subs = []
1214
}
1315

src/watcher.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ function Watcher (vm, expOrFn, cb, options) {
3737
this.id = ++uid // uid for batching
3838
this.active = true
3939
this.dirty = this.lazy // for lazy watchers
40-
this.deps = []
41-
this.newDeps = []
40+
this.deps = Object.create(null)
41+
this.newDeps = null
4242
this.prevError = null // for async error stacks
4343
// parse expression for getter/setter
4444
if (isFn) {
@@ -64,15 +64,12 @@ function Watcher (vm, expOrFn, cb, options) {
6464
*/
6565

6666
Watcher.prototype.addDep = function (dep) {
67-
var newDeps = this.newDeps
68-
var old = this.deps
69-
if (_.indexOf(newDeps, dep) < 0) {
70-
newDeps.push(dep)
71-
var i = _.indexOf(old, dep)
72-
if (i < 0) {
67+
var id = dep.id
68+
if (!this.newDeps[id]) {
69+
this.newDeps[id] = dep
70+
if (!this.deps[id]) {
71+
this.deps[id] = dep
7372
dep.addSub(this)
74-
} else {
75-
old[i] = null
7673
}
7774
}
7875
}
@@ -169,6 +166,7 @@ Watcher.prototype.set = function (value) {
169166

170167
Watcher.prototype.beforeGet = function () {
171168
Dep.target = this
169+
this.newDeps = Object.create(null)
172170
}
173171

174172
/**
@@ -177,17 +175,15 @@ Watcher.prototype.beforeGet = function () {
177175

178176
Watcher.prototype.afterGet = function () {
179177
Dep.target = null
180-
var oldDeps = this.deps
181-
var i = oldDeps.length
178+
var ids = Object.keys(this.deps)
179+
var i = ids.length
182180
while (i--) {
183-
var dep = oldDeps[i]
184-
if (dep) {
185-
dep.removeSub(this)
181+
var id = ids[i]
182+
if (!this.newDeps[id]) {
183+
this.deps[id].removeSub(this)
186184
}
187185
}
188186
this.deps = this.newDeps
189-
this.newDeps = oldDeps
190-
oldDeps.length = 0 // reuse old dep array
191187
}
192188

193189
/**
@@ -282,9 +278,10 @@ Watcher.prototype.evaluate = function () {
282278
*/
283279

284280
Watcher.prototype.depend = function () {
285-
var i = this.deps.length
281+
var depIds = Object.keys(this.deps)
282+
var i = depIds.length
286283
while (i--) {
287-
this.deps[i].depend()
284+
this.deps[depIds[i]].depend()
288285
}
289286
}
290287

@@ -300,9 +297,10 @@ Watcher.prototype.teardown = function () {
300297
if (!this.vm._isBeingDestroyed) {
301298
this.vm._watchers.$remove(this)
302299
}
303-
var i = this.deps.length
300+
var depIds = Object.keys(this.deps)
301+
var i = depIds.length
304302
while (i--) {
305-
this.deps[i].removeSub(this)
303+
this.deps[depIds[i]].removeSub(this)
306304
}
307305
this.active = false
308306
this.vm = this.cb = this.value = null

0 commit comments

Comments
 (0)