34

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?

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

3 Answers3

37

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.

Brandon
  • 9,822
  • 3
  • 27
  • 37
  • 1
    Suppose an operation is expensive. What will you do to fix it? Create a C++ extension for NodeJS and have ur JS call it? Or simply put the expensive work in a Worker Thread? Or something else? Maybe just create the app in another language? – Jiew Meng Mar 12 '13 at 01:08
  • 2
    Personally, my first effort would be to make the operation faster, as all of the other options add complexity. If that is not possible, I would consider setTimeout if we are looking at something taking only a couple of seconds and maybe split it into 2-4 chunks. If that is not enough, I would try a worker process. As a last resort, I may consider a C++ extension. – Brandon Mar 12 '13 at 02:04
  • You might want to check out webworker-threads or a similar package https://npmjs.org/package/webworker-threads – Brandon Mar 12 '13 at 02:45
11

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.

user568109
  • 47,225
  • 17
  • 99
  • 123
  • I could use Worker Threads, but it wont help much if they need to wait very long for it to complete? So I wonder, apart from the hanging UI (not hard to fix with worker threads), is it slower than say Python/Ruby to do the same computations? – Jiew Meng Mar 12 '13 at 01:05
  • That depends on how the implementation is done in both. JavaScript and python both are scripting languages. For general use JavaScript is faster than Python (Python is better in numerically intensive cases). See here http://www.quora.com/Programming-Languages/Is-JavaScript-v8-faster-than-Python. – user568109 Mar 12 '13 at 03:55
1

Bash scripting really gets into this. My professor is always harping on us about writing efficient code that will ease work on CPU

Here is a good example of inefficient practices in Linux

http://hacktux.com/bash/script/efficient

Another example I can think of is recursive functions, or functions that continually call themselves until a condition is satisfied. These typically take up a lot of CPU power.

onTheInternet
  • 6,421
  • 10
  • 41
  • 74