Measure Performance
Polaris provides a way to measure the performance of your application.
You can use the PolarisService
to create instruments that measure the time it takes to complete a specific operation.
React
export class GridComponent {
async onSort() {
// measure render time
const instrument = inject(PolarisService).getInstrument('grid-sort');
try {
instrument.start();
await sort();
requestAnimationFrame(() => instrument.done());
} catch (error) {
instrument.fail(error);
}
}
}
Let's review the code above.
- First, we inject the
PolarisService
into our component. - Then, we get an instrument by calling
getInstrument
with a unique name. - Next, we start the instrument.
- Then, we await the sort operation.
- We use
requestAnimationFrame
to ensure the sort operation has completed, and then we invoke thedone()
method on theInstrument
. - If the sort operation fails, we invoke the
fail()
method on theInstrument
.