Skip to content

Commit

Permalink
fix: stringify less when processing recordindgs
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Mar 14, 2024
1 parent 5b0c4d7 commit 263c96a
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ const getHrefFromSnapshot = (snapshot: RecordingSnapshot): string | undefined =>
return (snapshot.data as any)?.href || (snapshot.data as any)?.payload?.href
}

/*
cyrb53 (c) 2018 bryc (github.com/bryc)
License: Public domain. Attribution appreciated.
A fast and simple 53-bit string hash function with decent collision resistance.
Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
*/
const cyrb53 = function (str: string, seed = 0): number {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i)
h1 = Math.imul(h1 ^ ch, 2654435761)
h2 = Math.imul(h2 ^ ch, 1597334677)
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507)
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909)
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507)
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909)
return 4294967296 * (2097151 & h2) + (h1 >>> 0)
}

export const deduplicateSnapshots = (
newSnapshots?: RecordingSnapshot[],
existingSnapshots?: RecordingSnapshot[]
Expand All @@ -141,7 +162,11 @@ export const deduplicateSnapshots = (
// we can see duplicates that only differ by delay - these still count as duplicates
// even though the delay would hide that
const { delay: _delay, ...delayFreeSnapshot } = snapshot
const key = JSON.stringify(delayFreeSnapshot)
// we check each item multiple times as new snapshots come in
// so store the computer value on the object to save recalculating it so much
const key = (snapshot as any).seen || cyrb53(JSON.stringify(delayFreeSnapshot))
;(snapshot as any).seen = key

if (seenHashes.has(key)) {
return false
} else {
Expand Down

0 comments on commit 263c96a

Please sign in to comment.