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?
The main difference is that spawn
is more suitable for long-running processes with huge output. That's because spawn
streams input/output with a child process. On the other hand, exec
buffers output in a small (by default 1MB, 200 KB till v11.x) buffer. exec
first spawns a subshell, and then tries to execute your process. To cut a long story short, use spawn
in case you need a lot of data streamed from a child process and exec
if you need features like shell pipes, redirects or even more than one program at a time.
Some useful links - DZone Hacksparrow