Skip to content

Commit ea6f75a

Browse files
perf: optimize array deduplication in backup endpoints (#519)
- Replace spread operator with iterative Set.add() to avoid intermediate array allocation. - Add conditional assignment to avoid unnecessary updates when no new endpoints are found. - Improve memory efficiency and CPU performance during backup endpoint merging. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent 3882648 commit ea6f75a

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/client.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,14 @@ export class Pushy {
345345
const remoteEndpoints = await resp.json();
346346
log('fetch endpoints:', remoteEndpoints);
347347
if (Array.isArray(remoteEndpoints)) {
348-
server.backups = Array.from(
349-
new Set([...(server.backups || []), ...remoteEndpoints]),
350-
);
348+
const backups = server.backups || [];
349+
const set = new Set(backups);
350+
for (const endpoint of remoteEndpoints) {
351+
set.add(endpoint);
352+
}
353+
if (set.size !== backups.length) {
354+
server.backups = Array.from(set);
355+
}
351356
}
352357
} catch (e: any) {
353358
log('failed to fetch endpoints from: ', server.queryUrls);

0 commit comments

Comments
 (0)