Node和Node-Webkit之间的奇速差(〜10ms VS〜520ms)

我注意到在简单的node.js VS node-webkit之间速度的显着差异

我正在做的是使用HTTP模块连接到本地主机couchdb。

相同的代码需要约520ms才能build立连接,并在node-webkit中返回一个响应,而在普通的node.js中需要~10ms

有更深入的node-webkit知识的人可以帮助解释是什么造成的?

代码如下

Class = new function() {}; Class.prototype.info = function(s) {console.log(console,s);} Class.prototype.err = function(s) {console.log(console,s);} Class.prototype.warning = function(s) {console.log(console,s);} Class.prototype.debug = function(s) {console.log(console,s);} Class.prototype.postMessage = function(oterm, msg) { __cb.shift().call(this, msg); } Class.prototype.onMsgFor = {}; Class.prototype.__agentkeepalive = require('agentkeepalive'); Class.prototype.__http = require("http"); Class.prototype.__follow = require("follow"); Class.prototype.__http.globalAgent.maxSockets = 1; Class.prototype._onInit = function(props) { this._host = props["host"]; this._port = props["port"]; this._db_name = props["name"]; this._agent = new this.__agentkeepalive({ minSockets: 1 ,maxSockets: 1 , maxKeepAliveRequests: 0 , maxKeepAliveTime: 300000 }); }; Class.prototype.onMsgFor["connect_request_in"] = function(msg) { var err; var self = this; self._connect(this._host, this._port, this._db_name); }; /*************************************************************************/ Class.prototype._connect = function(host, port, namespace) { var self = this; var err; function _onConnect(code, body, hdrs) { function _onCreate(code, body, hdrs) { if (code == 201) { // created //self._registerChangeNotification(); self.postMessage("connect_response_out", {name: namespace}); } else { alert(code); err = "Unable to create namespace: " + namespace; self.error(err); self.postMessage("error_response_out", {text_content:err}); } } if (code == undefined) { // no code means error connecting self.error("Error connecting to DB: " + body); self.postMessage("error_response_out", {text_content:body ,question: "Is CouchDB up?"}); } else if (code == 200) { // ok if (body.indexOf(namespace) != -1) { //self._registerChangeNotification(); self.postMessage("connect_response_out", {name: namespace}); } else { self.warning("No such namespace: " + namespace); self._request(host, port, namespace, "PUT", _onCreate); } } else { alert("Unexpected code: " + code); } return; } self._request( host, port, "_all_dbs", "GET", _onConnect); }; Class.prototype._request = function(host, port, namespace, method, cb, uuid, arg, opts) { var t = (new Date()).getTime(); var self = this; var path = "/" + escape(namespace) + (uuid ? ("/" + escape(uuid)) : ""); var req, k, buf = "", headers = {}; if (method == "POST" || method == "PUT") { headers["content-type"] = "application/json"; buf = JSON.stringify(arg); } if (opts) { path += "?"; for (k in opts) { path += k + "=" + opts[k]; } } self.info("http://" + host + ":" + port + path); req = this.__http.request({hostname: host , port: port , path: path , method: method , headers : headers , agent: this._agent }); req.setNoDelay(true) req.setSocketKeepAlive(true, 0); function _onError(err) { cb(undefined, err.message, {}); } function _onSocket(socket) { console.log("[SOCKET: " + socket.localPort + "] , " + ((new Date()).getTime() - t) + " , W:" + socket.bytesWritten + " / R:" + socket.bytesRead); function _onEnd() { console.log("** END ** ") } function _onClose() { console.log("** CLOSE **"); } if (!socket.localPort) { socket.setNoDelay(true) socket.setKeepAlive(true, 0); socket.on("end", _onEnd); socket.on("close", _onClose); } } function _onResponse(response) { var len = response.headers["content-length"]; var encoding = response.headers["transfer-encoding"]; var payload = ""; console.log(" <<< [RESPONSE: " + response.statusCode+ "] " + (new Date()).getTime() + " , " + ((new Date()).getTime() - t)); function _onEnd() { switch (response.statusCode) { case 200: // ok case 201: // created try { payload = JSON.parse(payload || ""); } catch (e) { self.error("Error parsing payload"); cb(undefined,e.message,{}); return; /*****************************************************/ } break; case 400: // bad request break; case 404: // not found break; case 409: // conflict break; case 415: // Unsupported Media Type break; default: alert("ACK! unknown code: " + response.statusCode); break; } //console.log("PAYLOAD: " + JSON.stringify(payload)); cb(response.statusCode, payload, response.headers); } function _onClose() { console.log(">> CLOSE"); } function _onData(chunk) { payload += (chunk || ""); } self.debug("response returned in (ms): " + ((new Date()).getTime() - t)); // console.log((new Date()).getTime()); response.on("data", _onData); response.on("end", _onEnd); response.on("close", _onClose); } function _onClose() { console.log("[CLOSE]"); } function _onEnd() { console.log("[END]"); } req.on("socket", _onSocket); req.on("response", _onResponse); req.on("error", _onError); req.on("close", _onClose); req.on("end", _onEnd); console.log(" >>> [REQUEST: " + method + " " + path + "] " + t) req.end(buf); } var __cb = [ function(msg) { console.log("CONNECTED " + msg); } ]; var o = new Class(); o._onInit({host: "localhost" ,port: 5984 ,name: "stack"}); o.onMsgFor["connect_request_in"].call(o); 

谢谢

我当然不是专家,但可能是由于他们将节点运行时绑定到Webkit运行时。

节点具有高度优化,高效的运行时间,在任何时候都只有最必要的运行。

在webkit中(至less在服务器端编程方面)情况并非如此,因为在任何时候它都将具有渲染引擎,任何已加载的模块/扩展和页面脚本,以及css引擎等。

查看指向Node-Webkit wiki的链接,该链接解释了如何合并运行时: https : //github.com/rogerwang/node-webkit/wiki/How-node.js-is-integrated-with-chromium

希望这stream光了!