// Server-side HTML rendering: template literals, escaping, joins, and // conditional fragments over a list of typed view models. interface Product { id: number; title: string; description: string; price: number; tags: string[]; inStock: boolean; rating: number; } let seed = 31337; function random(): number { seed = (Math.imul(seed, 1103515245) + 12345) >>> 0; return seed / 4294967296; } const words = ["fast", "durable", "bold", "eco", '"quoted"', "light", "smart", "R&D", "classic", "modern"]; function sentence(n: number): string { const parts: string[] = []; for (let i = 0; i < n; i++) parts.push(words[Math.floor(random() * words.length)]!); return parts.join(" "); } function makeProducts(count: number): Product[] { const products: Product[] = []; for (let i = 0; i < count; i++) { products.push({ id: i, title: sentence(3), description: sentence(20), price: Math.round(random() * 100000) / 100, tags: [words[i % words.length]!, words[(i * 7) % words.length]!], inStock: random() < 0.8, rating: Math.round(random() * 50) / 10, }); } return products; } function escapeHtml(text: string): string { return text.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); } function stars(rating: number): string { let out = ""; for (let i = 1; i <= 5; i++) out += i <= Math.round(rating) ? "*" : "-"; return out; } function renderProduct(p: Product): string { const tags = p.tags.map((t) => `
  • ${escapeHtml(t)}
  • `).join(""); return `

    ${escapeHtml(p.title)}

    ${escapeHtml(p.description)}

    $${p.price.toFixed(2)} ${stars(p.rating)} ${p.inStock ? "" : "Sold out"}
    `; } function renderPage(products: Product[], page: number): string { const body = products.map(renderProduct).join("\n"); return `Page ${page}${body}`; } const scale = Number(process.argv[2] ?? "1"); const products = makeProducts(Math.floor(2000 * scale)); let bytes = 0; let hash = 0; for (let page = 0; page < 10; page++) { const html = renderPage(products, page); bytes += html.length; for (let i = 0; i < html.length; i += 101) hash = (hash * 33 + html.charCodeAt(i)) % 2147483647; } console.log("bytes", bytes, "hash", hash);