fix array filtering perf regression (revert 30bd8be)

This commit is contained in:
Evan You 2015-09-23 15:06:30 -04:00
parent f46e516907
commit d58a5b965b
3 changed files with 27 additions and 24 deletions

View File

@ -73,14 +73,17 @@ exports.orderBy = function (arr, sortKey, reverse) {
*/
function contains (val, search) {
var i
if (_.isPlainObject(val)) {
for (var key in val) {
if (contains(val[key], search)) {
var keys = Object.keys(val)
i = keys.length
while (i--) {
if (contains(val[keys[i]], search)) {
return true
}
}
} else if (_.isArray(val)) {
var i = val.length
i = val.length
while (i--) {
if (contains(val[i], search)) {
return true

View File

@ -1,4 +1,5 @@
var _ = require('../util')
var uid = 0
/**
* A dep is an observable that can have multiple
@ -8,6 +9,7 @@ var _ = require('../util')
*/
function Dep () {
this.id = uid++
this.subs = []
}

View File

@ -37,8 +37,8 @@ function Watcher (vm, expOrFn, cb, options) {
this.id = ++uid // uid for batching
this.active = true
this.dirty = this.lazy // for lazy watchers
this.deps = []
this.newDeps = []
this.deps = Object.create(null)
this.newDeps = null
this.prevError = null // for async error stacks
// parse expression for getter/setter
if (isFn) {
@ -64,15 +64,12 @@ function Watcher (vm, expOrFn, cb, options) {
*/
Watcher.prototype.addDep = function (dep) {
var newDeps = this.newDeps
var old = this.deps
if (_.indexOf(newDeps, dep) < 0) {
newDeps.push(dep)
var i = _.indexOf(old, dep)
if (i < 0) {
var id = dep.id
if (!this.newDeps[id]) {
this.newDeps[id] = dep
if (!this.deps[id]) {
this.deps[id] = dep
dep.addSub(this)
} else {
old[i] = null
}
}
}
@ -169,6 +166,7 @@ Watcher.prototype.set = function (value) {
Watcher.prototype.beforeGet = function () {
Dep.target = this
this.newDeps = Object.create(null)
}
/**
@ -177,17 +175,15 @@ Watcher.prototype.beforeGet = function () {
Watcher.prototype.afterGet = function () {
Dep.target = null
var oldDeps = this.deps
var i = oldDeps.length
var ids = Object.keys(this.deps)
var i = ids.length
while (i--) {
var dep = oldDeps[i]
if (dep) {
dep.removeSub(this)
var id = ids[i]
if (!this.newDeps[id]) {
this.deps[id].removeSub(this)
}
}
this.deps = this.newDeps
this.newDeps = oldDeps
oldDeps.length = 0 // reuse old dep array
}
/**
@ -282,9 +278,10 @@ Watcher.prototype.evaluate = function () {
*/
Watcher.prototype.depend = function () {
var i = this.deps.length
var depIds = Object.keys(this.deps)
var i = depIds.length
while (i--) {
this.deps[i].depend()
this.deps[depIds[i]].depend()
}
}
@ -300,9 +297,10 @@ Watcher.prototype.teardown = function () {
if (!this.vm._isBeingDestroyed) {
this.vm._watchers.$remove(this)
}
var i = this.deps.length
var depIds = Object.keys(this.deps)
var i = depIds.length
while (i--) {
this.deps[i].removeSub(this)
this.deps[depIds[i]].removeSub(this)
}
this.active = false
this.vm = this.cb = this.value = null