@@ -72,6 +72,7 @@ function $RootScopeProvider() {
72
72
var $rootScopeMinErr = minErr ( '$rootScope' ) ;
73
73
var lastDirtyWatch = null ;
74
74
var applyAsyncId = null ;
75
+ var watchId = 0 ;
75
76
76
77
this . digestTtl = function ( value ) {
77
78
if ( arguments . length ) {
@@ -80,6 +81,10 @@ function $RootScopeProvider() {
80
81
return TTL ;
81
82
} ;
82
83
84
+ function nextWatchId ( ) {
85
+ return ++ watchId ;
86
+ }
87
+
83
88
function createChildScopeClass ( parent ) {
84
89
function ChildScope ( ) {
85
90
this . $$watchers = this . $$nextSibling =
@@ -396,7 +401,8 @@ function $RootScopeProvider() {
396
401
last : initWatchVal ,
397
402
get : get ,
398
403
exp : prettyPrintExpression || watchExp ,
399
- eq : ! ! objectEquality
404
+ eq : ! ! objectEquality ,
405
+ id : nextWatchId ( )
400
406
} ;
401
407
402
408
lastDirtyWatch = null ;
@@ -414,10 +420,12 @@ function $RootScopeProvider() {
414
420
incrementWatchersCount ( this , 1 ) ;
415
421
416
422
return function deregisterWatch ( ) {
417
- if ( arrayRemove ( array , watcher ) >= 0 ) {
423
+ var index = binarySearch ( array , watcher . id ) ;
424
+ if ( index >= 0 ) {
425
+ array . splice ( index , 1 ) ;
418
426
incrementWatchersCount ( scope , - 1 ) ;
427
+ lastDirtyWatch = null ;
419
428
}
420
- lastDirtyWatch = null ;
421
429
} ;
422
430
} ,
423
431
@@ -1361,5 +1369,21 @@ function $RootScopeProvider() {
1361
1369
} ) ;
1362
1370
}
1363
1371
}
1372
+
1373
+ // Array is ordered in descending order by id
1374
+ function binarySearch ( array , id ) {
1375
+ var low = 0 ;
1376
+ var mid ;
1377
+ var high = array . length - 1 ;
1378
+ var value ;
1379
+ while ( low <= high ) {
1380
+ mid = ( low + high ) >>> 1 ;
1381
+ value = array [ mid ] . id ;
1382
+ if ( value > id ) low = mid + 1 ;
1383
+ else if ( value < id ) high = mid - 1 ;
1384
+ else return mid ;
1385
+ }
1386
+ return - ( low + 1 ) ;
1387
+ }
1364
1388
} ] ;
1365
1389
}
0 commit comments