Skip to content

Instantly share code, notes, and snippets.

@littensy
Created July 31, 2023 00:48
Show Gist options
  • Save littensy/1380d8a6a8e1d0c31daaf19fed9a2caa to your computer and use it in GitHub Desktop.
Save littensy/1380d8a6a8e1d0c31daaf19fed9a2caa to your computer and use it in GitHub Desktop.
Roblox React component profiler hack
import Roact, { FunctionComponent, createElement } from "@rbxts/roact";
export function profiler<P extends object>(name: string, render: FunctionComponent<P>): FunctionComponent<P> {
return (props: P) => {
debug.profilebegin(name);
const result = render(props);
debug.profileend();
return result;
};
}
export function profileAllComponents() {
const profiledComponents = new Map<FunctionComponent, FunctionComponent>();
const getName = (callback: Callback) => {
const [name = "Component"] = debug.info(callback, "n");
return name;
};
Roact.createElement = ((...args: Parameters<typeof Roact.createElement>) => {
const [component] = args;
if (typeIs(component, "function")) {
let profiledComponent = profiledComponents.get(component);
if (!profiledComponent) {
profiledComponent = profiler(getName(component), component);
profiledComponents.set(component, profiledComponent);
}
args[0] = profiledComponent as never;
}
return createElement(...args);
}) as Callback;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment