What do you count as a CPU intensive task. In terms of ... an algorithm/code for example (not so much a use case like video editing etc). Reason is it seems the main reason not to use NodeJS something I really like is mainly CPU intensive task. So what counts as that? Is it sorting, search, graph transversal, matrix multiply, for example?
So what are CPU Intensive tasks? They are complex user actions that eat up more RAM. A few of such processes can shut down your server entirely. Naturally, you want to make sure that your app or website is 'smart' enough to handle different kinds of tasks, for each individual user request.
The CPU performs basic arithmetic, logic, controlling, and input/output (I/O) operations specified by the instructions in the program. This contrasts with external components such as main memory and I/O circuitry, and specialized processors such as graphics processing units (GPUs).
Node. js uses a single thread to handle many clients, and while I/O operations can run asynchronously, CPU-intensive code can not. It means that too many (or too long) CPU-intensive tasks could keep the main thread too busy to handle other requests, practically blocking it.
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.
Processes or tasks which run on a computer require various resources like CPU cycles, memory, disk or network which are managed by the operating system, so that each task executes efficiently (without waiting for a resource if possible).
OS tries to maximize resource utilization by letting many processes use resources simultaneously. If a process requests a particular resource in large amount, it can bottleneck (delay) its execution. The process is said to be resource-intensive w.r.to that resource. So resource-intensive is a relative terminology.
Sorting, search, graph traversal, matrix multiply are all CPU operations, a process is CPU-intensive
or not it depends on how much and how frequent are their execution. For instance trans-coding video
or compressing files
is pretty CPU intensive, because they run the CPU operations far more than they need to read/write memory or disk. If you are planing on doing these, you should create a separate child process for it, so that it won't slowdown node process, which is single-threaded, or better create a node cluster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With