Skip to content

Instantly share code, notes, and snippets.

@schamane
Created February 23, 2013 09:47
Show Gist options
  • Save schamane/5019148 to your computer and use it in GitHub Desktop.
Save schamane/5019148 to your computer and use it in GitHub Desktop.
Curl vs http module for nodejs
var exec = require('child_process').exec,
url = "http://google.com/",
timeout = "3",
data="?q=test";
var time = process.hrtime();
exec('curl --max-time ' + timeout + ' -d \'' + data + '\' ' + url, function (error, stdout, stderr) {
var diff = process.hrtime(time);
//console.log('stdout: ' + stdout);
//console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
console.log("process took %d nanoseconds", diff[0] * 1e9 + diff[1]);
});
var http = require('http'),
data="?q=test",
options = {
hostname: 'www.google.com',
port: 80,
path: '/',
method: 'POST'
},
req, time;
req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
//console.log(chunk);
});
res.on('end', function() {
var diff = process.hrtime(time);
console.log("process took %d nanoseconds", diff[0] * 1e9 + diff[1]);
});
res.on('error', function(error) {
console.log('error: ' + error);
});
});
time = process.hrtime();
req.write(data);
req.end();
@schamane
Copy link
Author

node httprequest.js

process took 66766891 nanoseconds

node curl.js

process took 113138646 nanoseconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment