所以我在 Node.js 中创建了这个简单的测试服务器,
每当我进行直接响应时,我都会收到2200个请求/秒(快!)。当我只在它周围包裹一个简单的 Q 延迟时,它会下降到580个请求/秒(慢 4 倍!)。任何人都可以解释这种巨大的差异吗?
// 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);