@@ -45,8 +45,10 @@ export default function Watcher (vm, expOrFn, cb, options) {
4545 this . id = ++ uid // uid for batching
4646 this . active = true
4747 this . dirty = this . lazy // for lazy watchers
48- this . deps = Object . create ( null )
49- this . newDeps = null
48+ this . deps = [ ]
49+ this . newDeps = [ ]
50+ this . depIds = Object . create ( null )
51+ this . newDepIds = null
5052 this . prevError = null // for async error stacks
5153 // parse expression for getter/setter
5254 if ( isFn ) {
@@ -65,23 +67,6 @@ export default function Watcher (vm, expOrFn, cb, options) {
6567 this . queued = this . shallow = false
6668}
6769
68- /**
69- * Add a dependency to this directive.
70- *
71- * @param {Dep } dep
72- */
73-
74- Watcher . prototype . addDep = function ( dep ) {
75- var id = dep . id
76- if ( ! this . newDeps [ id ] ) {
77- this . newDeps [ id ] = dep
78- if ( ! this . deps [ id ] ) {
79- this . deps [ id ] = dep
80- dep . addSub ( this )
81- }
82- }
83- }
84-
8570/**
8671 * Evaluate the getter, and re-collect dependencies.
8772 */
@@ -179,7 +164,25 @@ Watcher.prototype.set = function (value) {
179164
180165Watcher . prototype . beforeGet = function ( ) {
181166 Dep . target = this
182- this . newDeps = Object . create ( null )
167+ this . newDepIds = Object . create ( null )
168+ this . newDeps . length = 0
169+ }
170+
171+ /**
172+ * Add a dependency to this directive.
173+ *
174+ * @param {Dep } dep
175+ */
176+
177+ Watcher . prototype . addDep = function ( dep ) {
178+ var id = dep . id
179+ if ( ! this . newDepIds [ id ] ) {
180+ this . newDepIds [ id ] = true
181+ this . newDeps . push ( dep )
182+ if ( ! this . depIds [ id ] ) {
183+ dep . addSub ( this )
184+ }
185+ }
183186}
184187
185188/**
@@ -188,15 +191,17 @@ Watcher.prototype.beforeGet = function () {
188191
189192Watcher . prototype . afterGet = function ( ) {
190193 Dep . target = null
191- var ids = Object . keys ( this . deps )
192- var i = ids . length
194+ var i = this . deps . length
193195 while ( i -- ) {
194- var id = ids [ i ]
195- if ( ! this . newDeps [ id ] ) {
196- this . deps [ id ] . removeSub ( this )
196+ var dep = this . deps [ i ]
197+ if ( ! this . newDepIds [ dep . id ] ) {
198+ dep . removeSub ( this )
197199 }
198200 }
201+ this . depIds = this . newDepIds
202+ var tmp = this . deps
199203 this . deps = this . newDeps
204+ this . newDepIds = tmp
200205}
201206
202207/**
@@ -291,10 +296,9 @@ Watcher.prototype.evaluate = function () {
291296 */
292297
293298Watcher . prototype . depend = function ( ) {
294- var depIds = Object . keys ( this . deps )
295- var i = depIds . length
299+ var i = this . deps . length
296300 while ( i -- ) {
297- this . deps [ depIds [ i ] ] . depend ( )
301+ this . deps [ i ] . depend ( )
298302 }
299303}
300304
@@ -311,10 +315,9 @@ Watcher.prototype.teardown = function () {
311315 if ( ! this . vm . _isBeingDestroyed && ! this . vm . _vForRemoving ) {
312316 this . vm . _watchers . $remove ( this )
313317 }
314- var depIds = Object . keys ( this . deps )
315- var i = depIds . length
318+ var i = this . deps . length
316319 while ( i -- ) {
317- this . deps [ depIds [ i ] ] . removeSub ( this )
320+ this . deps [ i ] . removeSub ( this )
318321 }
319322 this . active = false
320323 this . vm = this . cb = this . value = null
0 commit comments