Skip to content

Instantly share code, notes, and snippets.

@pi0
Last active November 7, 2024 13:59
Show Gist options
  • Save pi0/c3d8fb97c035cc4f2929b55ccb072a1a to your computer and use it in GitHub Desktop.
Save pi0/c3d8fb97c035cc4f2929b55ccb072a1a to your computer and use it in GitHub Desktop.
function reportRollupTiming(build) {
if (!build.getTimings) {
console.log(
"Rollup timing information is not available. Make sure `perf: true` config is set."
);
return;
}
const formatTime = (ms) =>
ms > 1000 ? `${(ms / 1000).toLocaleString()}s` : `${ms.toLocaleString()}ms`;
const timings = build.getTimings();
const timingGroups = [];
let currentGroup;
for (const key in timings) {
const time = timings[key][0];
const label = key.replace(/^[#-\s]+/, "");
if (key.startsWith("#")) {
currentGroup = { label, time, items: [] };
timingGroups.push(currentGroup);
} else {
currentGroup.items.push({ label, time });
}
}
let log = [];
for (const group of timingGroups) {
const totalMeasured = group.items.reduce((t, i) => t + i.time, 0);
if (group.time !== totalMeasured && group.items.length) {
group.items.push({
label: "(unknown)",
time: group.time - totalMeasured,
});
}
group.items.sort((a, b) => b.time - a.time);
log.push(`\n${group.label} (${formatTime(group.time)})`);
for (const item of group.items) {
log.push(` ${item.label.padEnd(40, " ")} ${formatTime(item.time)}`);
}
}
console.log(`\n${log.join("\n")}\n`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment