Multithreading?
As node.js is designed as a non-blocking single process, CPU-heavy work isn't distributed across multiple Cores. However, via the package 'child_process', it is natively able to start other node.js-processes which can then be managed by the OS.
Given the assumption that approximating a path is a long task, we could speed things up quite a bit by letting single processes work on one path at a time, which means with a 4-core-machine, 3 or 4 paths can be calculated in parallel!
The downside of this:
- It needs quite a bit of code to implement the work of child-processes
- Data between the processes is managed via 'messages'. This means, that we are sending a high amount of data between all these processes (parts of the bitmap, paths)
- Every child-process gets it's completely own V8 (necessary by design of the V8). This increases usage of system-resources. However, as we will probably start one process per core, this will not be that great of a problem...
So, the question is: is it worth the extra code, work and usage of system-resources? I tried it with an unoptimised recursive fibonacci-algorithm:
One process: fib(50)=12586269025 in 838499ms
Two processes: fib(50)=12586269025 in 250452ms