Take the 2-minute tour ×

Along the same lines as the rubygem Spork, can you fork a node process and have it be basically a complete clone of the current environment?

By "complete clone" I mean this cloned/child environment/process could reuse the existing require.cache, so the child process wouldn't have to take the (sometimes painful) performance hit of re-requiring the same modules again. Say, for example, require('lib-a') takes 2 seconds to load. How do you make it so when you call require('lib-a') in the child process, it is instant (aka using require.cache or something similar)?

A use case for this is in speeding up the startup time for a node express/connect HTTP server. In more complex apps, you may require a large amount of modules up front, sometimes taking a second or two to require all of them (not looking for info on how to lazy-load modules, I am making that optimization as well but it has its limitations).

The relevant Spork code is this:


I'm not sure what exactly it's doing with code like Marshal.dump(yield, @child_io) and Marshal.load(@child_io), but it looks like it's somehow copying the entire environment into the child process. By doing so, Spork made it so you can run 1 "main" Rails server, and "fork" it to run tests, so you don't have to wait (sometimes 10 seconds) for your Rails server to boot. How can you do that in node? Is it possible?


Is this considered a unix socket pair? This would help in the search.

share|improve this question

1 Answer 1

Node processes are intended to be long-lived, so I don't think startup time has ever specifically been a priority. This is sort of core to the philosophy of node. That being said, you might want to take a look at the cluster module. It allows you to fire up child worker processes. It is primarily designed for load balancing in multi-core scenarios, but may be of some help to you.

share|improve this answer

Your Answer


