@tanstack/table-devtools
Version:
Devtools for TanStack Table.
52 lines (50 loc) • 1.78 kB
JavaScript
import { bundleSizes } from "./bundleSizes.js";
//#region src/estimateBundleSize.ts
const BUNDLE_SIZE_GROUPS = [
"features",
"rowModels",
"filterFns",
"sortFns",
"aggregationFns"
];
/**
* Estimates the brotli size of table-core for a set of registered features,
* row models, and built-in fns.
*
* Per-item sizes overlap because items share code, so summing them
* overcounts. Instead, take the union of the declarations the items add
* (shared code counts once), convert that minified size to brotli with the
* measured size curve, and scale by how the selected items compress compared
* to the curve's prediction for them. Unknown names (custom features or fns)
* are ignored.
*/
function estimateBundleSize(selection, data = bundleSizes) {
const { core } = data;
const { chunks, coreMinified, curve } = data.estimate;
const [coefficient = 0, exponent = 1] = curve;
function curveSize(minified) {
return coefficient * minified ** exponent;
}
const unionChunks = /* @__PURE__ */ new Set();
let measured = 0;
let predicted = 0;
for (const group of BUNDLE_SIZE_GROUPS) {
const items = data.estimate[group];
const names = selection[group];
if (!items || !names) continue;
for (const name of new Set(names)) {
const item = items[name];
if (!item) continue;
measured += item[0] ?? 0;
predicted += item[1] ?? 0;
for (let i = 2; i < item.length; i++) unionChunks.add(item[i]);
}
}
if (!measured || !predicted) return core + measured;
let unionMinified = 0;
for (const chunk of unionChunks) unionMinified += chunks[chunk] ?? 0;
const compressionScale = measured / predicted;
return Math.round(core + (curveSize(coreMinified + unionMinified) - curveSize(coreMinified)) * compressionScale);
}
//#endregion
export { estimateBundleSize };