bvaughn / bvaughn/jest-react-profiler

Request: capture timings too

Open
#5 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
277
Forks
4
PR merge metrics
No merged PRs in 30d

Description

Hi, I would like to suggest a feature, can this library capture the timings too?

The `onRender` callback contains a number of interesting metrics; mainly `phase`, `baseDuration`, and `actualDuration`. It would be great if this profiler wrapper could capture these values. Here's an example of what I would like:

```tsx
const ExampleComponent = () =>

;
const TestComponent = withProfiler(ExampleComponent);

it("should capture all renders", () => {
const { rerender } = render();
rerender();

// `renders` is an array of the captured "phase" parameter
expect(TestComponent.profiler.renders).toMatchInlineSnapshot(`
Array [
"mount",
"update",
]
`);

// `timings` is an array of ALL the parameters:
console.log(TestComponent.profiler.timings);
// Logs:
// Array [
// {
// phase: "mount",
// actualDuration: 2.112893512389,
// baseDuration: 2.91723497584,
// startTime: 1147.7671834767779,
// commitTime: 1157.7768147817643,
// },
// { phase: "update", ... }
// ]

TestComponent.profiler.clear();
});
```

I have 2 use-cases:
1. Better assertions. In the example above, I'm not limited to just using `.toHaveCommittedTimes(#)`. By exposing the number of renders via `Wrapper.profiler.renders`, I'm able to make any assertions I want, including snapshots or range testing.

2. Quick access to rendering times. The `Wrapper.profiler.timings` array would contain the detailed timings for each render. I don't intend to use these values to make assertions (because this kind of perf testing is too unstable), but I do think it could be useful for logging the timings for testing purposes. It would be great to know whether `mount` or `update` is taking the most time, and it would be a nice developer tool for attempting to improve performance.

What do you think? (Fwiw, I've got a forked copy that has these implemented, if you'd like to see it)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.