Просмотр исходного кода

fix array filtering perf regression (revert 30bd8be)

Evan You 10 лет назад
Родитель
Сommit
2a41b3a0bd
3 измененных файлов с 26 добавлено и 22 удалено
  1. 6 3
      src/filters/array-filters.js
  2. 2 0
      src/observer/dep.js
  3. 18 19
      src/watcher.js

+ 6 - 3
src/filters/array-filters.js

@@ -73,14 +73,17 @@ exports.orderBy = function (arr, sortKey, reverse) {
  */
  */
 
 
 function contains (val, search) {
 function contains (val, search) {
+  var i
   if (_.isPlainObject(val)) {
   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
         return true
       }
       }
     }
     }
   } else if (_.isArray(val)) {
   } else if (_.isArray(val)) {
-    var i = val.length
+    i = val.length
     while (i--) {
     while (i--) {
       if (contains(val[i], search)) {
       if (contains(val[i], search)) {
         return true
         return true

+ 2 - 0
src/observer/dep.js

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

+ 18 - 19
src/watcher.js

@@ -37,7 +37,7 @@ function Watcher (vm, expOrFn, cb, options) {
   this.id = ++uid // uid for batching
   this.id = ++uid // uid for batching
   this.active = true
   this.active = true
   this.dirty = this.lazy // for lazy watchers
   this.dirty = this.lazy // for lazy watchers
-  this.deps = []
+  this.deps = Object.create(null)
   this.newDeps = null
   this.newDeps = null
   this.prevError = null // for async error stacks
   this.prevError = null // for async error stacks
   // parse expression for getter/setter
   // parse expression for getter/setter
@@ -64,15 +64,12 @@ function Watcher (vm, expOrFn, cb, options) {
  */
  */
 
 
 Watcher.prototype.addDep = function (dep) {
 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)
       dep.addSub(this)
-    } else {
-      old[i] = null
     }
     }
   }
   }
 }
 }
@@ -150,7 +147,7 @@ Watcher.prototype.set = function (value) {
 
 
 Watcher.prototype.beforeGet = function () {
 Watcher.prototype.beforeGet = function () {
   Dep.target = this
   Dep.target = this
-  this.newDeps = []
+  this.newDeps = Object.create(null)
 }
 }
 
 
 /**
 /**
@@ -159,15 +156,15 @@ Watcher.prototype.beforeGet = function () {
 
 
 Watcher.prototype.afterGet = function () {
 Watcher.prototype.afterGet = function () {
   Dep.target = null
   Dep.target = null
-  var i = this.deps.length
+  var ids = Object.keys(this.deps)
+  var i = ids.length
   while (i--) {
   while (i--) {
-    var dep = this.deps[i]
-    if (dep) {
-      dep.removeSub(this)
+    var id = ids[i]
+    if (!this.newDeps[id]) {
+      this.deps[id].removeSub(this)
     }
     }
   }
   }
   this.deps = this.newDeps
   this.deps = this.newDeps
-  this.newDeps = null
 }
 }
 
 
 /**
 /**
@@ -262,9 +259,10 @@ Watcher.prototype.evaluate = function () {
  */
  */
 
 
 Watcher.prototype.depend = function () {
 Watcher.prototype.depend = function () {
-  var i = this.deps.length
+  var depIds = Object.keys(this.deps)
+  var i = depIds.length
   while (i--) {
   while (i--) {
-    this.deps[i].depend()
+    this.deps[depIds[i]].depend()
   }
   }
 }
 }
 
 
@@ -280,9 +278,10 @@ Watcher.prototype.teardown = function () {
     if (!this.vm._isBeingDestroyed) {
     if (!this.vm._isBeingDestroyed) {
       this.vm._watchers.$remove(this)
       this.vm._watchers.$remove(this)
     }
     }
-    var i = this.deps.length
+    var depIds = Object.keys(this.deps)
+    var i = depIds.length
     while (i--) {
     while (i--) {
-      this.deps[i].removeSub(this)
+      this.deps[depIds[i]].removeSub(this)
     }
     }
     this.active = false
     this.active = false
     this.vm = this.cb = this.value = null
     this.vm = this.cb = this.value = null