4 ms·
I might be missing something, what code practices do you have in mind that help with CPU profiling?
by williamdclt 2y ago
I might be missing something, what code practices do you have in mind that help with CPU profiling?
- moralestapia 2y agoThings that help: * No anonymous functions/lambdas (unless they're extremely trivial, but then you probably don't need that in a function at all) * Avoid recursion * Functions do one simple thing and return * Only one return at the end of the function body * No uncaught exceptions * Functions that "await" and functions that "compute" are separate ones * Avoid 3rd party libraries unless it is absolutely necessary * Write code for a single thread model, scale horizontally with cluster, things like pm2 or just running several node processes Never had to write them down, tbh, there's definitely many more. This could apply to every language, though, not just JS. You could borrow some practices from realtime computing, as well. If you do most of these things, debugging with console.trace is straightforward. You can also use one of the flamegraph tools out there for the profiling part. They only thing you can't control (but you can examine to some extent) is the GC, but in practice that's never been an issue for me since the GC that comes with V8 is really good.