-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimise has many notifications (#4850)
* optimise has-many notifications * bring in line with master * bring in line with master * exit flushCanonical early if not alive * add test for non contiguous change * niggles * jshint fix
- Loading branch information
1 parent
c53b8f8
commit 03440c1
Showing
4 changed files
with
1,417 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
@namespace | ||
@method diff-array | ||
@for DS | ||
@param {Array} oldArray the old array | ||
@param {Array} newArray the new array | ||
@return {hash} { | ||
firstChangeIndex: <integer>, // null if no change | ||
addedCount: <integer>, // 0 if no change | ||
removedCount: <integer> // 0 if no change | ||
} | ||
*/ | ||
export default function diffArray(oldArray, newArray) { | ||
const oldLength = oldArray.length; | ||
const newLength = newArray.length; | ||
|
||
const shortestLength = Math.min(oldLength, newLength); | ||
let firstChangeIndex = null; // null signifies no changes | ||
|
||
// find the first change | ||
for (let i=0; i<shortestLength; i++) { | ||
// compare each item in the array | ||
if (oldArray[i] !== newArray[i]) { | ||
firstChangeIndex = i; | ||
break; | ||
} | ||
} | ||
|
||
if (firstChangeIndex === null && newLength !== oldLength) { | ||
// no change found in the overlapping block | ||
// and array lengths differ, | ||
// so change starts at end of overlap | ||
firstChangeIndex = shortestLength; | ||
} | ||
|
||
let addedCount = 0; | ||
let removedCount = 0; | ||
if (firstChangeIndex !== null) { | ||
// we found a change, find the end of the change | ||
let unchangedEndBlockLength = shortestLength - firstChangeIndex; | ||
// walk back from the end of both arrays until we find a change | ||
for (let i=1; i<=shortestLength; i++) { | ||
// compare each item in the array | ||
if (oldArray[oldLength-i] !== newArray[newLength-i]) { | ||
unchangedEndBlockLength = i-1; | ||
break; | ||
} | ||
} | ||
addedCount = newLength - unchangedEndBlockLength - firstChangeIndex; | ||
removedCount = oldLength - unchangedEndBlockLength - firstChangeIndex; | ||
} | ||
|
||
return { | ||
firstChangeIndex, | ||
addedCount, | ||
removedCount | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.