From 6d4c4b1bdd911d1ce901cf3b7a519dcb2f95f4c0 Mon Sep 17 00:00:00 2001 From: Yonatan Lavy <123459744+yonatanlavy@users.noreply.github.com> Date: Thu, 17 Apr 2025 02:14:57 +0300 Subject: [PATCH] refactor: improve performance of renderFilesToXml using Array.join (#127) ### Summary Refactored the `renderFilesToXml` function to improve performance and readability by replacing iterative string concatenation with `Array.map().join()`. ### Changes - Replaced the `for...of` loop with `files.map(...).join('')` - Reduced number of string mutation operations - Preserved the existing XML structure and CDATA safety ### Why Using `join` avoids repeated string concatenation in loops, which can improve performance, especially when rendering a large number of files. It also results in more concise and idiomatic code. I have read the CLA Document and I hereby sign the CLA --- Let me know if this needs any adjustments! Signed-off-by: yonatanlavy --- codex-cli/src/utils/singlepass/context.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/codex-cli/src/utils/singlepass/context.ts b/codex-cli/src/utils/singlepass/context.ts index e185ba9d..20f32cca 100644 --- a/codex-cli/src/utils/singlepass/context.ts +++ b/codex-cli/src/utils/singlepass/context.ts @@ -51,14 +51,15 @@ export function renderTaskContext(taskContext: TaskContext): string { * For each file, we embed the content in a CDATA section. */ function renderFilesToXml(files: Array): string { - let xmlContent = ""; - for (const fc of files) { - xmlContent += ` + const fileContents = files + .map( + (fc) => ` ${fc.path} - `; - } - xmlContent += "\n"; - return xmlContent; + `, + ) + .join(""); + + return `\n${fileContents}\n`; }