storybook-addon-performance
Version:
A storybook addon to help better understand and debug performance for React components
45 lines (39 loc) • 1.03 kB
JavaScript
import {
invariant
} from "./chunk-WOOEBIFK.mjs";
// src/task-runner/with-duration.ts
async function getDuration(fn) {
const start = performance.now();
await fn();
const finish = performance.now();
return finish - start;
}
async function withDuration(fn) {
let timedDuration = null;
let isControlled = false;
const controls = {
time: async function time(fn2) {
invariant(!isControlled, "controls.time has already been used");
isControlled = true;
timedDuration = await getDuration(fn2);
}
};
const wholeTaskDuration = await getDuration(() => fn(controls));
if (isControlled) {
invariant(
timedDuration != null,
`
You have used controls.timed but have not waited for the result to finish
Ensure that you wait for the result:
await controls.time(async () => {});
You might not be waiting for controls.time
controls.time(async () => {});
`
);
return timedDuration;
}
return wholeTaskDuration;
}
export {
withDuration
};