Skip to content

Instantly share code, notes, and snippets.

@chamerling
Created December 16, 2022 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chamerling/6c5a11850570961c7c2e66890bce263b to your computer and use it in GitHub Desktop.
Save chamerling/6c5a11850570961c7c2e66890bce263b to your computer and use it in GitHub Desktop.
import { ComponentType, useEffect, useRef } from "react";
const withWebPerformance = <
T extends object & { onLoaded?: (measure: PerformanceMeasure) => void }
>(
Component: ComponentType<T>,
name: string
) => {
return (props: T) => {
const { onLoaded } = props;
const ref = useRef(0);
useEffect(() => {
ref.current++;
});
useEffect(() => {
if (ref.current === 1) {
return;
}
console.log(performance.mark(name));
return () => {
const measure = performance.measure(name, name);
console.log(measure);
onLoaded?.(measure);
};
}, [onLoaded]);
return <Component {...props} />;
};
};
export { withWebPerformance };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment