Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Last active February 16, 2022 07:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sindresorhus/83711353bfcf6770bdb37357e8d2d83c to your computer and use it in GitHub Desktop.
Save sindresorhus/83711353bfcf6770bdb37357e8d2d83c to your computer and use it in GitHub Desktop.
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
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