Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 214
Amr M. AbdulRahman Avatar asked Nov 30 '25 00:11

Amr M. AbdulRahman


1 Answers

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.

like image 197
bolav Avatar answered Dec 04 '25 23:12

bolav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!