@@ -37,7 +37,7 @@ function Watcher (vm, expOrFn, cb, options) {
37
37
this . id = ++ uid // uid for batching
38
38
this . active = true
39
39
this . dirty = this . lazy // for lazy watchers
40
- this . deps = [ ]
40
+ this . deps = Object . create ( null )
41
41
this . newDeps = null
42
42
this . prevError = null // for async error stacks
43
43
// parse expression for getter/setter
@@ -64,15 +64,12 @@ function Watcher (vm, expOrFn, cb, options) {
64
64
*/
65
65
66
66
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
73
72
dep . addSub ( this )
74
- } else {
75
- old [ i ] = null
76
73
}
77
74
}
78
75
}
@@ -150,7 +147,7 @@ Watcher.prototype.set = function (value) {
150
147
151
148
Watcher . prototype . beforeGet = function ( ) {
152
149
Dep . target = this
153
- this . newDeps = [ ]
150
+ this . newDeps = Object . create ( null )
154
151
}
155
152
156
153
/**
@@ -159,15 +156,15 @@ Watcher.prototype.beforeGet = function () {
159
156
160
157
Watcher . prototype . afterGet = function ( ) {
161
158
Dep . target = null
162
- var i = this . deps . length
159
+ var ids = Object . keys ( this . deps )
160
+ var i = ids . length
163
161
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 )
167
165
}
168
166
}
169
167
this . deps = this . newDeps
170
- this . newDeps = null
171
168
}
172
169
173
170
/**
@@ -262,9 +259,10 @@ Watcher.prototype.evaluate = function () {
262
259
*/
263
260
264
261
Watcher . prototype . depend = function ( ) {
265
- var i = this . deps . length
262
+ var depIds = Object . keys ( this . deps )
263
+ var i = depIds . length
266
264
while ( i -- ) {
267
- this . deps [ i ] . depend ( )
265
+ this . deps [ depIds [ i ] ] . depend ( )
268
266
}
269
267
}
270
268
@@ -280,9 +278,10 @@ Watcher.prototype.teardown = function () {
280
278
if ( ! this . vm . _isBeingDestroyed ) {
281
279
this . vm . _watchers . $remove ( this )
282
280
}
283
- var i = this . deps . length
281
+ var depIds = Object . keys ( this . deps )
282
+ var i = depIds . length
284
283
while ( i -- ) {
285
- this . deps [ i ] . removeSub ( this )
284
+ this . deps [ depIds [ i ] ] . removeSub ( this )
286
285
}
287
286
this . active = false
288
287
this . vm = this . cb = this . value = null
0 commit comments