What counts as CPU Intensive tasks (eg. sorting, searching etc?)

Terms like “intensive” or “expensive” are relative and it isn’t always obvious what activities are CPU-intensive. Generally speaking, anything that isn’t I/O is CPU. And I/O is asynchronous in node.js, so not a problem. Therefore, we are left with everything except for I/O being expensive.

Your approach to pick general patterns is wise. Sorting, searching, and even algorithms in general are CPU-bound. Of course, you can’t eliminate CPU usage, but if you can make your database sort instead of your app code, you may be better off.

I would also keep an eye out for large loops. A loop that doesn’t fire any asynchronous events is a bottleneck. Of course, one cannot entirely avoid loops. They are a fact of life for programming. If your loops are short, then no problem. If you find a loop that runs 10,000 times, you may want to consider breaking it up using setTimeout, process.nextTick, or a separate node process.

10,000 was picked arbitrarily. It depends on what the loop does. Your milage may vary.

Leave a Comment

tech