Created
July 31, 2023 00:48
-
-
Save littensy/1380d8a6a8e1d0c31daaf19fed9a2caa to your computer and use it in GitHub Desktop.
Roblox React component profiler hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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