Node js curl vs http.request

I need to send an HTTP request to another server. I can do it in two ways: 1) using http.request () 2) using child_process.exec

// ... define timeout, data, url
var __exec = require('child_process').exec;
exec('curl --max-time ' + timeout + ' -d \'' + data + '\' '  + url, function (error, stdout, stderr) {});

      

In the first case, the minimum execution time is 0.08 seconds. In the second case - 0.04 seconds

What problems can arise if I use the second option? Especially in case of high server load.

Thank.

Benchmark1:

//...
timeStart = +new Date().getTime();
request = http.request(options, function (result) {
    //...
    result.on('end', function () {
         timeEnd = (+new Date().getTime() - timeStart) / 1000;
         // log timeEnd
    });
});
request.on('error', function (error) {
    timeEnd = (+new Date().getTime() - timeStart) / 1000;
    // log timeEnd
});
request.end();

      

Benchmark2:

// ...
timeStart = +new Date().getTime();
exec('curl --max-time ' + timeout + ' -d \'' + data + '\' ' + url, function (error, stdout, stderr) {
     timeEnd = (+new Date().getTime() - timeStart) / 1000;
     // log timeEnd
 });

      

+3


source to share





All Articles