From 09388f430aaaec54d6e1a7b30247f0eed48e3484 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:49:46 +0000 Subject: [PATCH] perf: resource optimization and latency benchmarks - Hoist ESCAPE_MAP in index.mjs and privacy.mjs to avoid redundant object creation during html escaping - Cache toLowerCase() string in validateDirectory - Run file reading and writing concurrently using Promise.all in build.mjs - Add a rendering speed benchmark test suite Co-authored-by: teddashh <233386770+teddashh@users.noreply.github.com> --- scripts/build.mjs | 20 ++++++++++++++------ src/index.mjs | 8 +++++--- src/privacy.mjs | 3 ++- test/benchmark.test.mjs | 27 +++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 test/benchmark.test.mjs diff --git a/scripts/build.mjs b/scripts/build.mjs index 626831c..72a5690 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -1,13 +1,21 @@ import { readFile, mkdir, writeFile } from 'node:fs/promises'; import { renderDirectory } from '../src/index.mjs'; import { renderPrivacyPage } from '../src/privacy.mjs'; -const directory = JSON.parse(await readFile(new URL('../data/directory.json', import.meta.url), 'utf8')); -const privacy = JSON.parse(await readFile(new URL('../data/privacy-discord-bot.json', import.meta.url), 'utf8')); +const [directoryFile, privacyFile] = await Promise.all([ + readFile(new URL('../data/directory.json', import.meta.url), 'utf8'), + readFile(new URL('../data/privacy-discord-bot.json', import.meta.url), 'utf8') +]); +const directory = JSON.parse(directoryFile); +const privacy = JSON.parse(privacyFile); // Validate both inputs, including the privacy output path, before any writes. const directoryHtml = renderDirectory(directory); const privacyHtml = renderPrivacyPage(privacy); -await mkdir(new URL('../dist/', import.meta.url), { recursive: true }); -await writeFile(new URL('../dist/index.html', import.meta.url), directoryHtml); -await mkdir(new URL(`../dist/${privacy.path}`, import.meta.url), { recursive: true }); -await writeFile(new URL(`../dist/${privacy.path}index.html`, import.meta.url), privacyHtml); +await Promise.all([ + mkdir(new URL('../dist/', import.meta.url), { recursive: true }).then(() => + writeFile(new URL('../dist/index.html', import.meta.url), directoryHtml) + ), + mkdir(new URL(`../dist/${privacy.path}`, import.meta.url), { recursive: true }).then(() => + writeFile(new URL(`../dist/${privacy.path}index.html`, import.meta.url), privacyHtml) + ) +]); console.log(`Built source directory at dist/index.html and privacy page at dist/${privacy.path}index.html; deployment is done by .github/workflows/pages.yml on main.`); diff --git a/src/index.mjs b/src/index.mjs index 86d178b..0488115 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,12 +1,14 @@ -const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[character]); +const ESCAPE_MAP = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; +const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ESCAPE_MAP[character]); export function validateDirectory(input) { if (input?.schema_version !== 'freedom.source-directory/v1' || input.organization !== 'FreeTWAI' || !Array.isArray(input.projects) || input.projects.length > 100) throw new TypeError('Expected the FreeTWAI public source directory'); const seen = new Set(); for (const project of input.projects) { if (!project || !/^(?:\.github|[A-Za-z0-9][A-Za-z0-9._-]{0,99})$/.test(project.repository ?? '') || project.repository.includes('..')) throw new TypeError('Invalid repository name'); - if (seen.has(project.repository.toLowerCase())) throw new TypeError('Duplicate repository'); - seen.add(project.repository.toLowerCase()); + const repoLower = project.repository.toLowerCase(); + if (seen.has(repoLower)) throw new TypeError('Duplicate repository'); + seen.add(repoLower); for (const key of ['name', 'description', 'scope']) if (typeof project[key] !== 'string' || !project[key].trim() || project[key].length > 500) throw new TypeError(`Invalid ${key}`); } return input; diff --git a/src/privacy.mjs b/src/privacy.mjs index a649bc0..0dd4323 100644 --- a/src/privacy.mjs +++ b/src/privacy.mjs @@ -1,4 +1,5 @@ -const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[character]); +const ESCAPE_MAP = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; +const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ESCAPE_MAP[character]); // Escape first, then link only https URLs and plain e-mail addresses (no raw HTML from data). const linkify = (text) => escapeHtml(text) diff --git a/test/benchmark.test.mjs b/test/benchmark.test.mjs new file mode 100644 index 0000000..0e43fa7 --- /dev/null +++ b/test/benchmark.test.mjs @@ -0,0 +1,27 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import { renderDirectory } from '../src/index.mjs'; +import { renderPrivacyPage } from '../src/privacy.mjs'; + +test('performance benchmark: renderDirectory executes quickly over thousands of iterations', async () => { + const directory = JSON.parse(await readFile(new URL('../data/directory.json', import.meta.url), 'utf8')); + const start = performance.now(); + for (let i = 0; i < 5000; i++) { + renderDirectory(directory); + } + const end = performance.now(); + const duration = end - start; + assert.ok(duration < 2000, `renderDirectory 5000 iterations took ${duration.toFixed(2)}ms, expected under 2000ms`); +}); + +test('performance benchmark: renderPrivacyPage executes quickly over thousands of iterations', async () => { + const privacy = JSON.parse(await readFile(new URL('../data/privacy-discord-bot.json', import.meta.url), 'utf8')); + const start = performance.now(); + for (let i = 0; i < 5000; i++) { + renderPrivacyPage(privacy); + } + const end = performance.now(); + const duration = end - start; + assert.ok(duration < 2000, `renderPrivacyPage 5000 iterations took ${duration.toFixed(2)}ms, expected under 2000ms`); +});