Vì vậy, tôi đã tạo máy chủ thử nghiệm đơn giản này trong Node.js
Bất cứ khi nào tôi trả lời trực tiếp, tôi nhận được yêu cầu yêu cầu/giây (nhanh!). Khi tôi chỉ quấn Q đơn giản được hoãn lại, nó giảm xuống còn yêu cầu/giây (4 lần chậm hơn!). Ai có thể giải thích rằng sự khác biệt lớn?Làm thế nào để Q trì hoãn quá chậm trên Node.js?
// Requires
var server = require('http');
var q = require('q');
// Start server
var http = require('http');
http.createServer(function(request, response) {
// Comment out either of two below sections
// Without deferred
// 2200 reqs/second
response.writeHead(200, {"Content-Type": "text/html"});
response.write("test");
response.end();
// Q deferred
// 580 reqs/second
var deferred = q.defer();
deferred.promise.then(function() {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("test");
response.end();
});
deferred.resolve();
}).listen(1234);
Dường như lời hứa Q thực sự chậm, tôi chỉ đọc [this] (http://dailyjs.com/2013/03/27/node-roundup/) đề cập đến [Thư viện nguyện] (https: // github. com/dfilatov/jspromise) dường như nhanh hơn 50 lần so với Q. – robertklep
Tôi đang kiểm tra https://github.com/medikoo/deferred vào lúc này, và dường như không có phí nào (hoặc nhận được ở mức 2200) reqs/giây đều đặn). Tôi thực sự tò mò tại sao Q quá chậm ... –
Duyệt qua mã Tôi nhận thấy nó sử dụng 'process.nextTick()' rất nhiều, điều này có thể làm chậm đáng kể mọi thứ. – robertklep