From b0ac4714e7925a736690af069f01cfb2962f8a56 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:45:46 +0000 Subject: [PATCH] Performance pass: resource optimization and latency benchmarks - Optimized `escapeHtml` function in `scripts/build.mjs` using a pre-compiled map and RegExp check (`reHasUnescapedHtml.test`) instead of unconditionally running string replace for HTML escapes. - Optimized server caching in `scripts/serve.mjs` by introducing a memory cache (`Map`) for file contents. - Added performance benchmark test in `tests/benchmark.test.mjs` to ensure `renderWorkspace` meets expected latencies (< 200ms for 10000 iterations). - Updated `package.json` to run all tests in the `tests` directory. Co-authored-by: teddashh <233386770+teddashh@users.noreply.github.com> --- package.json | 2 +- scripts/build.mjs | 8 +++++++- scripts/serve.mjs | 10 +++++++++- tests/benchmark.test.mjs | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/benchmark.test.mjs diff --git a/package.json b/package.json index b98d5dc..1bd7149 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "engines": {"node": ">=24"}, "scripts": { "build": "node scripts/build.mjs", - "test": "node --test tests/workspace.test.mjs", + "test": "node --test tests/*.test.mjs", "test:browser": "playwright test", "preview": "node scripts/serve.mjs" }, diff --git a/scripts/build.mjs b/scripts/build.mjs index 6f507e5..5d0e9e7 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -18,7 +18,13 @@ export function validateWorkspace(value) { } return value; } -export const escapeHtml = value => String(value).replace(/[&<>"']/g, char => ({'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}[char])); +const htmlEscapes = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; +const reUnescapedHtml = /[&<>"']/g; +const reHasUnescapedHtml = RegExp(reUnescapedHtml.source); +export const escapeHtml = value => { + const str = String(value); + return (str && reHasUnescapedHtml.test(str)) ? str.replace(reUnescapedHtml, (chr) => htmlEscapes[chr]) : str; +}; const href = (workspace, action) => `https://freetwai.com/#guilds/${workspace.guild_key}/${action}`; export function renderWorkspace(workspace) { validateWorkspace(workspace); diff --git a/scripts/serve.mjs b/scripts/serve.mjs index 786ca66..23905eb 100644 --- a/scripts/serve.mjs +++ b/scripts/serve.mjs @@ -2,10 +2,18 @@ import {createServer} from 'node:http'; import {readFile} from 'node:fs/promises'; import {resolve} from 'node:path'; const types = {'/index.html': 'text/html; charset=utf-8', '/site.js': 'text/javascript; charset=utf-8', '/style.css': 'text/css; charset=utf-8', '/brand.png': 'image/png', '/release.json': 'application/json'}; +const cache = new Map(); const server = createServer(async (request, response) => { const path = request.url === '/' ? '/index.html' : request.url; if (!Object.hasOwn(types, path)) {response.writeHead(404).end(); return;} - try {response.writeHead(200, {'Content-Type': types[path], 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff'}).end(await readFile(resolve('dist', '.' + path)));} + try { + let data = cache.get(path); + if (!data) { + data = await readFile(resolve('dist', '.' + path)); + cache.set(path, data); + } + response.writeHead(200, {'Content-Type': types[path], 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff'}).end(data); + } catch {response.writeHead(404).end();} }); server.listen(4417, '127.0.0.1', () => process.stdout.write('Preview http://127.0.0.1:4417\n')); diff --git a/tests/benchmark.test.mjs b/tests/benchmark.test.mjs new file mode 100644 index 0000000..8da1fb1 --- /dev/null +++ b/tests/benchmark.test.mjs @@ -0,0 +1,16 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { renderWorkspace } from '../scripts/build.mjs'; +import { readFile } from 'node:fs/promises'; + +const workspace = JSON.parse(await readFile(new URL('../workspace.json', import.meta.url), 'utf8')); + +test('renderWorkspace performance', () => { + const start = performance.now(); + for (let i = 0; i < 10000; i++) { + renderWorkspace(workspace); + } + const end = performance.now(); + const time = end - start; + assert.ok(time < 200, `renderWorkspace took ${time}ms for 10000 iterations, expected < 200ms`); +});