Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Results are as expected. Async comes with a slight overhead because of the event loop, but has many other benefits. So unless you know you won't need it, go for async.

Measuring sync vs async child processes in Node.js

  • macOS 10.12.3
  • Node.js 7.4.0

childProcess

const childProcess = require('child_process');

console.time('sync');
childProcess.execFileSync('networksetup', ['-listallhardwareports']);
console.timeEnd('sync');

console.time('async');
childProcess.execFile('networksetup', ['-listallhardwareports'], () => {
	console.timeEnd('async');
});
sync: 25.707ms
async: 39.501ms

execa

const execa = require('execa');

console.time('sync');
execa.sync('networksetup', ['-listallhardwareports']);
console.timeEnd('sync');

console.time('async');
await execa('networksetup', ['-listallhardwareports']);
console.timeEnd('async');
sync: 25.263ms
async: 45.735ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment