Skip to content

Commit 2a41b3a

Browse files
committed
fix array filtering perf regression (revert 30bd8be)
1 parent 0d59333 commit 2a41b3a

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
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: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ 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 = []
40+
this.deps = Object.create(null)
4141
this.newDeps = null
4242
this.prevError = null // for async error stacks
4343
// parse expression for getter/setter
@@ -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
}
@@ -150,7 +147,7 @@ Watcher.prototype.set = function (value) {
150147

151148
Watcher.prototype.beforeGet = function () {
152149
Dep.target = this
153-
this.newDeps = []
150+
this.newDeps = Object.create(null)
154151
}
155152

156153
/**
@@ -159,15 +156,15 @@ Watcher.prototype.beforeGet = function () {
159156

160157
Watcher.prototype.afterGet = function () {
161158
Dep.target = null
162-
var i = this.deps.length
159+
var ids = Object.keys(this.deps)
160+
var i = ids.length
163161
while (i--) {
164-
var dep = this.deps[i]
165-
if (dep) {
166-
dep.removeSub(this)
162+
var id = ids[i]
163+
if (!this.newDeps[id]) {
164+
this.deps[id].removeSub(this)
167165
}
168166
}
169167
this.deps = this.newDeps
170-
this.newDeps = null
171168
}
172169

173170
/**
@@ -262,9 +259,10 @@ Watcher.prototype.evaluate = function () {
262259
*/
263260

264261
Watcher.prototype.depend = function () {
265-
var i = this.deps.length
262+
var depIds = Object.keys(this.deps)
263+
var i = depIds.length
266264
while (i--) {
267-
this.deps[i].depend()
265+
this.deps[depIds[i]].depend()
268266
}
269267
}
270268

@@ -280,9 +278,10 @@ Watcher.prototype.teardown = function () {
280278
if (!this.vm._isBeingDestroyed) {
281279
this.vm._watchers.$remove(this)
282280
}
283-
var i = this.deps.length
281+
var depIds = Object.keys(this.deps)
282+
var i = depIds.length
284283
while (i--) {
285-
this.deps[i].removeSub(this)
284+
this.deps[depIds[i]].removeSub(this)
286285
}
287286
this.active = false
288287
this.vm = this.cb = this.value = null

0 commit comments

Comments
 (0)