Node.js Spawn vs. Execute

Harry Cramer picture Harry Cramer · Feb 9, 2018 · Viewed 20.7k times · Source

In an online training video I am watching to learn Node, the narrator says that "spawn is better for longer processes involving large amounts of data, whereas execute is better for short bits of data."

Why is this? What is the difference between the child_process spawn and execute functions in Node.js, and when do I know which one to use?

Answer

Vasyl Moskalov picture Vasyl Moskalov · Feb 9, 2018

The main difference is the spawn is more suitable for long-running process with huge output. spawn streams input/output with child process. exec buffered output in a small (by default 200K) buffer. Also as I know exec first spawn subshell, then try to execute your process. To cut long story short use spawn in case you need a lot of data streamed from child process and exec if you need such features as shell pipes, redirects or even you need exec more than one program in one time.

Some useful links - DZone Hacksparrow