Something went wrong. Try again.
Demo comparing html-over-the-wire w/ WS and SSE.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108// Test 6 — why the gap exists: the sliding window, not codec folklore.//// If the brotli advantage were just "brotli is a better algorithm", shrinking// brotli's window would not close it. It does. The variable that matters for// repeated HTML is whether the previous copy of the markup is still inside the// window when the next one is encoded.//// DEFLATE's window is capped at 32 KB by the format itself (windowBits <= 15).// That cap is the whole story for WebSocket, because permessage-deflate is the// only compression WebSocket has.
import zlib from 'node:zlib'import { makeBoard, apply, renderPatch, renderBoard } from '../src/domain.js'
const PATCHES = 60
function stream(mode) { const board = makeBoard() const out = [] for (let i = 0; i < PATCHES; i++) { const row = apply(board, { id: i, nonce: i + 1 }) out.push(Buffer.from(renderPatch(board, row, mode), 'utf8')) } return out}
async function run(make, flushCode, patches) { const z = make() let acc = 0 z.on('data', (c) => (acc += c.length)) const per = [] let prev = 0 for (const p of patches) { z.write(p) await new Promise((res) => z.flush(flushCode, res)) per.push(acc - prev) prev = acc } await new Promise((res) => z.end(res)) return { total: acc, warm: per.slice(-10).reduce((a, b) => a + b, 0) / 10 }}
const patches = stream('fat')const payload = Buffer.byteLength(renderBoard(makeBoard()))console.log(`\n=== Test 6: window size vs payload size ===\n`)console.log(`One board render = ${(payload / 1024).toFixed(1)} KB. For patch N to compress against`)console.log(`patch N-1, the window must be at least that large.\n`)
const rows = []
for (const bits of [9, 11, 13, 15]) { const r = await run( () => zlib.createDeflateRaw({ level: 6, windowBits: bits }), zlib.constants.Z_SYNC_FLUSH, patches, ) rows.push({ codec: `deflate w=${bits}`, window: 1 << bits, warm: r.warm, note: bits === 15 ? 'DEFLATE FORMAT MAXIMUM — WebSocket stops here' : '', })}
for (const lgwin of [10, 16, 17, 18, 20, 22, 24]) { const r = await run( () => zlib.createBrotliCompress({ params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 5, [zlib.constants.BROTLI_PARAM_LGWIN]: lgwin, [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT, }, }), zlib.constants.BROTLI_OPERATION_FLUSH, patches, ) rows.push({ codec: `brotli lgwin=${lgwin}`, window: 1 << lgwin, warm: r.warm, note: (1 << lgwin) < payload ? 'window < one board — behaves like deflate' : 'window >= one board — previous copy is reachable', })}
const w = (s, n) => String(s).padEnd(n)console.log(w('codec', 20) + w('window', 12) + w('warm patch', 14) + 'note')console.log('─'.repeat(100))for (const r of rows) { console.log( w(r.codec, 20) + w(r.window >= 1048576 ? `${r.window / 1048576} MB` : `${r.window / 1024} KB`, 12) + w(`${r.warm.toFixed(0)} B`, 14) + r.note, )}
console.log( `\nThe cliff sits exactly where the window crosses the payload size. WebSocket\n` + `cannot get to the far side of it: 32 KB is the DEFLATE format's ceiling, and\n` + `permessage-deflate is the only compression the protocol defines.\n`,)
console.log(JSON.stringify({ test: 'window', payload, rows }))