From 263c96a60e9157457ccd21f8bcc4a874a21970a3 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Thu, 14 Mar 2024 23:54:24 +0000 Subject: [PATCH] fix: stringify less when processing recordindgs --- .../player/sessionRecordingDataLogic.ts | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingDataLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingDataLogic.ts index e8eabbdb645d1..8d630aa10acbb 100644 --- a/frontend/src/scenes/session-recordings/player/sessionRecordingDataLogic.ts +++ b/frontend/src/scenes/session-recordings/player/sessionRecordingDataLogic.ts @@ -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[] @@ -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 {