diff options
| author | Hazelnoot <acomputerdog@gmail.com> | 2025-05-25 12:33:05 -0400 |
|---|---|---|
| committer | Hazelnoot <acomputerdog@gmail.com> | 2025-05-28 21:31:40 -0400 |
| commit | 8a2ed3bc86e9531038686d911c3b0975272c911f (patch) | |
| tree | ce7e89e8d2201c0ce3aecb814995b6b1e27e0723 /packages/backend/src/misc | |
| parent | add TS caches to gitignore (diff) | |
| download | sharkey-8a2ed3bc86e9531038686d911c3b0975272c911f.tar.gz sharkey-8a2ed3bc86e9531038686d911c3b0975272c911f.tar.bz2 sharkey-8a2ed3bc86e9531038686d911c3b0975272c911f.zip | |
minor optimization to diff-arrays
Diffstat (limited to 'packages/backend/src/misc')
| -rw-r--r-- | packages/backend/src/misc/diff-arrays.ts | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/packages/backend/src/misc/diff-arrays.ts b/packages/backend/src/misc/diff-arrays.ts index 1f6820481b..b3879cc996 100644 --- a/packages/backend/src/misc/diff-arrays.ts +++ b/packages/backend/src/misc/diff-arrays.ts @@ -10,8 +10,9 @@ export interface DiffResult<T> { /** * Calculates the difference between two snapshots of data. - * Null, undefined, and empty arrays are supported, and values do not have to be unique. + * Null, undefined, and empty arrays are supported, and duplicate values are ignored. * Result sets are de-duplicated, and will be empty if no data was added or removed (respectively). + * The inputs are treated as un-ordered, so a re-ordering of the same data will NOT be considered a change. * @param dataBefore Array containing data before the change * @param dataAfter Array containing data after the change */ @@ -26,7 +27,8 @@ export function diffArrays<T>(dataBefore: T[] | null | undefined, dataAfter: T[] for (const host of before) { // before and NOT after => removed - if (!after.has(host)) { + // delete operation removes duplicates to speed up the "after" loop + if (!after.delete(host)) { removed.push(host); } } |