From 80e9fd51eb62fff6fe2d9aafcdc4d5c7de2dea84 Mon Sep 17 00:00:00 2001 From: mkorwel Date: Mon, 9 Feb 2026 16:44:28 -0600 Subject: [PATCH] perf: avoid double stringification in ChatRecordingService --- .../core/src/services/chatRecordingService.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/core/src/services/chatRecordingService.ts b/packages/core/src/services/chatRecordingService.ts index 75551eca16..99d5381b0b 100644 --- a/packages/core/src/services/chatRecordingService.ts +++ b/packages/core/src/services/chatRecordingService.ts @@ -461,9 +461,18 @@ export class ChatRecordingService { // when nothing has changed. const currentContent = JSON.stringify(conversation, null, 2); if (this.cachedLastConvData !== currentContent) { - // Only update the timestamp and re-stringify if something actually changed. - conversation.lastUpdated = new Date().toISOString(); - const finalContent = JSON.stringify(conversation, null, 2); + // To avoid a second full stringification for a large conversation object, + // we can replace just the timestamp in the already stringified content. + // This is significantly more performant. + const newTimestamp = new Date().toISOString(); + const finalContent = currentContent.replace( + `"lastUpdated": "${conversation.lastUpdated}"`, + `"lastUpdated": "${newTimestamp}"`, + ); + + // Update the in-memory object's timestamp to stay in sync. + conversation.lastUpdated = newTimestamp; + this.cachedLastConvData = finalContent; fs.writeFileSync(this.conversationFile, finalContent); }