@@ -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
6666Watcher . 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
151148Watcher . 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
160157Watcher . 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
264261Watcher . 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