Skip to content

Commit 484858a

Browse files
JLHwungmathiasbynens
authored andcommitted
perf: compress inline data with brotli instead of gzip
Switch `gzipInline` to `brotliInline`, using maximum quality and the largest window so long-range repetition (e.g. in `Names/index.mjs`) is exploited. Generated modules now import `brotliDecompressSync` from `node:zlib`. For Unicode 18.0.0 this shrinks the package from 700 KB to 590 KB packed (1.96 MB to 1.79 MB unpacked); `Names/index.mjs` goes from 228 KB to 158 KB. Assisted-By: Claude Fable 5.1 Issue: #109
1 parent b596066 commit 484858a

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

‎scripts/utils.mjs‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ import { encodeRanges, encodeRegenerate } from './encode-ranges.mjs';
88

99
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1010

11-
const gzipInline = (data) => {
11+
const brotliInline = (data) => {
1212
if (data instanceof Map) {
13-
return `new Map(${gzipInline([...data])})`;
13+
return `new Map(${brotliInline([...data])})`;
1414
}
1515
const json = JSON.stringify(data);
16-
const gzipBuffer = zlib.gzipSync(json);
17-
const str = gzipBuffer.toString('base64');
18-
return `JSON.parse(gunzipSync(Buffer.from('${str}', 'base64')))`;
16+
const brotliBuffer = zlib.brotliCompressSync(json, {
17+
params: {
18+
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
19+
[zlib.constants.BROTLI_PARAM_LGWIN]:
20+
zlib.constants.BROTLI_MAX_WINDOW_BITS,
21+
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: Buffer.byteLength(json),
22+
},
23+
});
24+
const str = brotliBuffer.toString('base64');
25+
return `JSON.parse(brotliDecompressSync(Buffer.from('${str}', 'base64')))`;
1926
};
2027

2128
const range = (start, stop) => {
@@ -164,7 +171,7 @@ const writeFiles = (options) => {
164171
// Sequence properties are special.
165172
if (type === 'Sequence_Property' || isNameAliases) {
166173
const sequences = codePoints;
167-
const output = `import { gunzipSync } from 'node:zlib';\n\nexport default ${gzipInline(sequences)};\n`;
174+
const output = `import { brotliDecompressSync } from 'node:zlib';\n\nexport default ${brotliInline(sequences)};\n`;
168175
writeFileIfChanged(path.resolve(dir, 'index.mjs'), output);
169176
writeFileIfChanged(
170177
path.resolve(dir, 'index.d.mts'),
@@ -222,8 +229,8 @@ const writeFiles = (options) => {
222229
codePointsFileContent = `export default ${jsesc(codePoints)};\n`;
223230
symbolsFileContent = `export default ${jsesc(symbols)};\n`;
224231
} else {
225-
codePointsFileContent = `import { gunzipSync } from 'node:zlib';\n\nexport default ${gzipInline(codePoints)};\n`;
226-
symbolsFileContent = `import { gunzipSync } from 'node:zlib';\n\nexport default ${gzipInline(symbols)};\n`;
232+
codePointsFileContent = `import { brotliDecompressSync } from 'node:zlib';\n\nexport default ${brotliInline(codePoints)};\n`;
233+
symbolsFileContent = `import { brotliDecompressSync } from 'node:zlib';\n\nexport default ${brotliInline(symbols)};\n`;
227234
}
228235
if (
229236
(type === 'Case_Folding' && item === 'F') ||
@@ -264,7 +271,7 @@ const writeFiles = (options) => {
264271
}
265272
fs.mkdirSync(dir, { recursive: true });
266273
// `Bidi_Mirroring_Glyph/index.mjs`
267-
// Note: `Bidi_Mirroring_Glyph` doesn’t have repeated strings; don’t gzip.
274+
// Note: `Bidi_Mirroring_Glyph` doesn’t have repeated strings; don’t compress.
268275
const output = [
269276
`const chr = String.fromCodePoint;`,
270277
`const pair = (t, u, v) => [t ? u + v : v, chr(t ? u : u + v)];`,
@@ -295,7 +302,7 @@ const writeFiles = (options) => {
295302
// or `bidi-brackets/index.mjs`
296303
// or `Names/index.mjs`
297304
const flatRuns = samePropertyRuns(auxMap[type]);
298-
const output = `import { gunzipSync } from 'node:zlib';\nimport decodePropertyMap from '../decode-property-map.mjs';\n\nexport default decodePropertyMap(${gzipInline(
305+
const output = `import { brotliDecompressSync } from 'node:zlib';\nimport decodePropertyMap from '../decode-property-map.mjs';\n\nexport default decodePropertyMap(${brotliInline(
299306
flatRuns,
300307
)});\n`;
301308
writeFileIfChanged(path.resolve(dir, 'index.mjs'), output);

0 commit comments

Comments
 (0)