@@ -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
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}
@@ -169,6 +166,7 @@ Watcher.prototype.set = function (value) {
169166
170167Watcher . 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
178176Watcher . 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
284280Watcher . 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