From 05893dd8d098373f7b2fe3627b9a9fe95e102a1b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 10:44:41 +0000 Subject: [PATCH] performance: optimize escapeHtml memory allocation, site.js object checking, and parallelize build IO - Move htmlEntities out of escapeHtml to prevent recreating object on every match - Avoid Object.keys().sort().join() in postMessage handler to reduce intermediate string and array allocations - Parallelize writing generated static site assets in build.mjs with Promise.all() - Add rendering benchmark test file to prevent regressions Co-authored-by: teddashh <233386770+teddashh@users.noreply.github.com> --- scripts/build.mjs | 18 +++++++++++------- src/site.js | 3 ++- tests/benchmark.test.mjs | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 tests/benchmark.test.mjs diff --git a/scripts/build.mjs b/scripts/build.mjs index 6f507e5..ef22da5 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -18,7 +18,8 @@ export function validateWorkspace(value) { } return value; } -export const escapeHtml = value => String(value).replace(/[&<>"']/g, char => ({'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}[char])); +const htmlEntities = {'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}; +export const escapeHtml = value => String(value).replace(/[&<>"']/g, char => htmlEntities[char]); const href = (workspace, action) => `https://freetwai.com/#guilds/${workspace.guild_key}/${action}`; export function renderWorkspace(workspace) { validateWorkspace(workspace); @@ -41,12 +42,15 @@ export async function build(root = process.cwd(), sourceSha) { const output = resolve(root, 'dist'); await mkdir(output, {recursive: true}); const serialized = JSON.stringify(workspace).replaceAll('<', '\\u003c').replaceAll('\u2028', '\\u2028').replaceAll('\u2029', '\\u2029'); - await writeFile(resolve(output, 'index.html'), renderWorkspace(workspace)); - await writeFile(resolve(output, 'site.js'), `const workspace = ${serialized};\nconst sourceSha = ${JSON.stringify(sha)};\n` + await readFile(resolve(root, 'src/site.js'), 'utf8')); - await copyFile(resolve(root, 'src/style.css'), resolve(output, 'style.css')); - await copyFile(resolve(root, 'brand.png'), resolve(output, 'brand.png')); - await writeFile(resolve(output, '.nojekyll'), ''); - await writeFile(resolve(output, 'release.json'), JSON.stringify({schema_version: 'freedom.guild-release/v1', guild_key: workspace.guild_key, repository: workspace.repository, source_sha: sha, bridge: 'freedom.guild-launchpad/v1'}) + '\n'); + const siteJsSourcePromise = readFile(resolve(root, 'src/site.js'), 'utf8'); + await Promise.all([ + writeFile(resolve(output, 'index.html'), renderWorkspace(workspace)), + siteJsSourcePromise.then(siteJsSource => writeFile(resolve(output, 'site.js'), `const workspace = ${serialized};\nconst sourceSha = ${JSON.stringify(sha)};\n` + siteJsSource)), + copyFile(resolve(root, 'src/style.css'), resolve(output, 'style.css')), + copyFile(resolve(root, 'brand.png'), resolve(output, 'brand.png')), + writeFile(resolve(output, '.nojekyll'), ''), + writeFile(resolve(output, 'release.json'), JSON.stringify({schema_version: 'freedom.guild-release/v1', guild_key: workspace.guild_key, repository: workspace.repository, source_sha: sha, bridge: 'freedom.guild-launchpad/v1'}) + '\n') + ]); return output; } if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { diff --git a/src/site.js b/src/site.js index 85af622..aef0256 100644 --- a/src/site.js +++ b/src/site.js @@ -22,7 +22,8 @@ if (event.source !== window.parent || !['https://freetwai.com', 'https://staging.freetwai.com'].includes(event.origin) && !/^http:\/\/(?:127\.0\.0\.1|localhost):\d{1,5}$/.test(event.origin)) return; const value = event.data; - if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).sort().join(',') !== 'actions,channel,guildKey,protocol,theme,type' + if (!value || typeof value !== 'object' || Array.isArray(value) + || Object.keys(value).length !== 6 || !('actions' in value && 'channel' in value && 'guildKey' in value && 'protocol' in value && 'theme' in value && 'type' in value) || value.protocol !== protocol || value.type !== 'init' || value.guildKey !== workspace.guild_key || typeof value.channel !== 'string' || !/^[a-f0-9-]{36}$/.test(value.channel) || !['light', 'night', 'versefolk'].includes(value.theme) diff --git a/tests/benchmark.test.mjs b/tests/benchmark.test.mjs new file mode 100644 index 0000000..e15e0d9 --- /dev/null +++ b/tests/benchmark.test.mjs @@ -0,0 +1,19 @@ +import {test} from 'node:test'; +import assert from 'node:assert/strict'; +import {readFile} from 'node:fs/promises'; +import {renderWorkspace} from '../scripts/build.mjs'; + +const workspace = JSON.parse(await readFile(new URL('../workspace.json', import.meta.url), 'utf8')); + +test('renderWorkspace performance meets latency requirements', () => { + const iterations = 10000; + const start = performance.now(); + for (let i = 0; i < iterations; i++) { + renderWorkspace(workspace); + } + const end = performance.now(); + const duration = end - start; + + // Upper bound for 10000 renders should be well within 500ms + assert.ok(duration < 500, `renderWorkspace took ${duration}ms for ${iterations} iterations, exceeding 500ms limit`); +});