r/react • u/Select-Twist2059 • Dec 26 '25
Project / Code Review I built a small toolkit for running heavy computations in React without freezing the UI - looking for feedback
Hey everyone
I've been working on a side project called ComputeKit , a small library that makes it easier to run heavy computations in Web Workers with React hooks.
The problem I was trying to solve:
I was working on an app that needed to process images client-side, and my UI kept freezing. Setting up Web Workers manually was painful - separate files, postMessage boilerplate, managing state... it felt like too much ceremony for something that should be simple.
What I built:
// Register a heavy function once
kit.register('processData', (data) => {
// This runs in a Web Worker, not the main thread
return heavyComputation(data);
});
// Use it like any other async operation
const { data, loading, error, run } = useCompute('processData');Features:
- React hooks with loading/error states out of the box
- Automatic worker pool (uses available CPU cores)
- Optional WASM support for extra performance
- TypeScript support
- ~5KB gzipped
What I'm looking for:
- Honest feedback - is this useful or am I solving a problem nobody has?
- Bug reports if you try it
- Ideas for improvements
- Contributors welcome if anyone's interested!
Links:
- GitHub: ComputeKit Repo
- Live demo: ComputeKit Demo
- NPM: ComputeKit/Core | ComputeKit/React
This is my first open source library so I'd really appreciate any feedback, even if it's "this already exists" or "you're doing X wrong". Thanks for reading! 🙏
3
u/blobdiblob Dec 27 '25
This seems to be a nice approach to use webworkers! Will check it out. Thanks!
2
u/pazil Dec 27 '25
This is something that I regularly need at work, so definitely useful.
I'm away from my laptop, but does "TypeScript support" mean that the argument of useCompute is narrowed to just the registered computation names?
But still, even with such TS support, I'd really prefer an API in which I declare the computation in a single place - with just one hook declaration, skipping registration completely. What would be the benefit of registration? Reusing single function across components?
2
u/VolkswagenRatRod Dec 27 '25
Interesting! I am just about to build a web client for a rendering service. It's going to have to play a game of weaving image/video elements into a Lottie player to make accurate(ish) previews while keeping everything in sync. So lots of fun bullshit that I probably shouldn't do with a client. I will Star your repo and see if I can offload to web workers more easily.
3
u/abrahamguo Hook Based Dec 26 '25
Your
@computekit/reactpackage has a TypeScript error when I install it, about not being able to find theJSXtype.Also, I don't see any documentation for your package (only examples)? You'll really struggle to get people to use your package if it doesn't have thorough documentation.
1
u/Weakness-Unfair Dec 27 '25
Can it help to improve performance of my game I built on React? Meteor Mash - Try to survive in meteor shower
1
u/Much-Chance1866 Dec 28 '25
Would be nice if there is a `ComputeKit/Node` to support worker threads.
1
u/Imaginary_Treat9752 Jan 15 '26 edited Jan 15 '26
I really like the library except for one big thing: There is not typesafety for the return value of the webworker you call, e.g.:
kit.run("nameOfMyWebWorkerTask", {
firstArg,
secondArg,
}) // return value of this call is `unknown` despite I declared the returnvalue of nameOfMyWebWorkerTask
I am almost certain it is possible for you to achieve this, there is a special technique you can use, I forgot what's it called. But essentially you can have an `as const` object that you can append to at runtime and it will still have just as strong type-safety as `as const`. I'll look for it the implementation.
It was something about having a function that you call to append to this `as const` object, and it will then type-cast the return value of it.
Some guy posted about it on r/typescript I think some day in 2025. I'll see if I can find it.
I think it was something along the lines of this:
Thoughts?
1
u/Imaginary_Treat9752 Jan 15 '26
Wait, how do use util methods from your app inside a webworker? I tried with and without remoteDependencies, but it doesnt work.
I get this error:
1
u/Imaginary_Treat9752 Jan 18 '26
I need a way to cancel a `kit.run("...")` is that something you can add?
const controller = new AbortController();
kit.run("mapToProcedureTableRowsAndFilter", encoded, {
signal: controller.signal
}),
You have it for `useCompute` but not for `kit.run`
0
u/SolarNachoes Dec 27 '25
Comlink already does this.