Amr M. AbdulRahman
Amr M. AbdulRahman

Reputation: 1980

Why NodeJS execSync() is too slow when executing custom scripts

I'm working on a server to server communication that must be synchronous on the client. I ended up using child_process.execSync()

The problem now is that execSync() is too slow executing node.js custom script file, for example:

// main.js
var res = execSync('node --version');
// TIME: 8 ms

Although, having a custom node script that only prints out the node version to the stdout, like:

// script.js:
process.stdout.write(process.version);
process.exit();

then:

// main.js
var res = execSync('node script.js');
// TIME: 135 ms !!!

this is almost 16x. What am I missing?

Upvotes: 3

Views: 2115

Answers (1)

bolav
bolav

Reputation: 6998

I'm unable to reproduce your numbers on my system. This is running on my Macbook Pro, home computer. Calling your first main.js for main1.js and second for main2.js and just adding var execSync = require('child_process').execSync; on the top of main1 and main2.

$ time node main1.js
v5.3.0


real    0m0.099s
user    0m0.075s
sys 0m0.022s

$ time node script.js
v5.3.0
real    0m0.075s
user    0m0.058s
sys 0m0.015s

$ time node main2.js
v5.3.0

real    0m0.166s
user    0m0.131s
sys 0m0.031s

Those numbers looks good to me.

Upvotes: 1

Related Questions