2 answers

★ Best answer

It really depends on what you're doing exactly, but there are some things i'd check right away. first of all see if you're querying the database without indexes - that's one of the most common causes of slowdowns. if you use mongodb check that your find() calls have the right indexes, same thing with postgresql. then look if you're doing too many synchronous operations when you could be using async/await properly, maybe you're loading entire files into memory when you could be streaming, or you're doing n+1 queries when one with a join would be enough. another thing: if you use express make sure you don't have useless middleware or middleware doing heavy operations unnecessarily.

then i'd say do real profiling, not just guessing. use node --prof to generate an isolate file, then analyze it with node --prof-process. or use clinic.js which is easier to read, or just throw console.time() around your code to see where time is spent. sometimes you discover the bottleneck is where you'd least expect it, like some library you installed that does something heavy behind the scenes. if you're doing a lot of heavy computations also think about worker threads to avoid blocking the event loop.

oh and obviously, update node to a stable version if you're behind, make sure you have enough memory available and that you're not leaking memory (monitor with process.memoryUsage()). sometimes the problem isn't even the code but your hosting configuration or the number of db connections you're opening. start with profiling and from there you'll understand what's actually eating up your resources.

Profiling is your best weapon - use Node.js's built-in profiler (or clinic.js if you want something more powerful) to see where you're actually spending time, because the bottleneck often isn't where you think it is. Once you've done that, 90% of the time it's a combination of slow database queries, callbacks piling up, or synchronous file I/O that needs to go.

Your answer

Log into answer.