我使用最新的Node.js v8.9.3向RESTful API发出请求,而且我在性能方面的结果令人失望。
我得到的最好结果是使用节点大约800毫秒,而使用curl或来自C#的简单两行请求从浏览器完成相同请求大约需要300毫秒。
我已经尝试了本地http和https模块的请求,还有一些其他库(req-fast,node-fetch,甚至模拟XMLHttpRequest)无济于事,这些其他选项甚至更慢。
我做错了吗?有什么可以做的不同,或者我们只是假设节点包装器无论什么都慢?
我使用的示例代码:
var start = new Date();
var options = {
host: 'www.binance.com',
port: 443,
path: '/api/v1/ticker/24hr?symbol=ETHBTC',
method: 'GET',
};
https.get(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
var end = new Date() - start;
console.info("Execution time: %dms", end);
});
}).on('error', (e) => {
console.error(e);
});
结果:~830ms
Chrome的结果: chrome screenshot