Browse Source

Replace slow and broken for..in loops with faster for loops over the keys.

v0.7.4-release
Tim Caswell 15 years ago
committed by Ryan Dahl
parent
commit
62d9852c3d
  1. 8
      lib/child_process.js
  2. 19
      lib/dns.js
  3. 16
      lib/fs.js
  4. 60
      lib/http.js
  5. 37
      lib/ini.js
  6. 18
      lib/querystring.js
  7. 6
      lib/url.js
  8. 8
      src/node.js

8
lib/child_process.js

@ -76,10 +76,10 @@ ChildProcess.prototype.spawn = function (path, args, env) {
args = args || []; args = args || [];
env = env || process.env; env = env || process.env;
var envPairs = []; var envPairs = [];
for (var key in env) { var keys = Object.keys(env);
if (env.hasOwnProperty(key)) { for (var index = 0, keysLength = keys.length; index < keysLength; index++) {
envPairs.push(key + "=" + env[key]); var key = keys[index];
} envPairs.push(key + "=" + env[key]);
} }
var fds = this._internal.spawn(path, args, envPairs); var fds = this._internal.spawn(path, args, envPairs);

19
lib/dns.js

@ -8,7 +8,9 @@ var activeWatchers = {};
var timer = new process.Timer(); var timer = new process.Timer();
timer.callback = function () { timer.callback = function () {
for (var socket in activeWatchers) { var sockets = Object.keys(activeWatchers);
for (var i = 0, l = sockets.length; i < l; i++) {
var socket = sockets[i];
var s = parseInt(socket); var s = parseInt(socket);
channel.processFD( watchers[socket].read ? s : dns.SOCKET_BAD channel.processFD( watchers[socket].read ? s : dns.SOCKET_BAD
, watchers[socket].write ? s : dns.SOCKET_BAD , watchers[socket].write ? s : dns.SOCKET_BAD
@ -21,13 +23,16 @@ timer.callback = function () {
function updateTimer() { function updateTimer() {
timer.stop(); timer.stop();
for (var socket in activeWatchers) { // if !empty(activeWatchers) // Were just checking to see if activeWatchers is empty or not
var max = 20000; for (var socket in activeWatchers) {
var timeout = channel.timeout(max); if (activeWatchers.hasOwnProperty(socket)) {
var max = 20000;
timer.start(timeout, 0); var timeout = channel.timeout(max);
return; timer.start(timeout, 0);
// Short circuit the loop on first find.
return;
}
} }
} }

16
lib/fs.js

@ -490,7 +490,13 @@ var FileReadStream = fs.FileReadStream = function(path, options) {
this.bufferSize = 4 * 1024; this.bufferSize = 4 * 1024;
options = options || {}; options = options || {};
for (var i in options) this[i] = options[i];
// Mixin options into this
var keys = Object.keys(options);
for (var index = 0, length = keys.length; index < length; index++) {
var key = keys[index];
this[key] = options[key];
}
var self = this; var self = this;
@ -621,7 +627,13 @@ var FileWriteStream = fs.FileWriteStream = function(path, options) {
this.mode = 0666; this.mode = 0666;
options = options || {}; options = options || {};
for (var i in options) this[i] = options[i];
// Mixin options into this
var keys = Object.keys(options);
for (var index = 0, length = keys.length; index < length; index++) {
var key = keys[index];
this[key] = options[key];
}
this.busy = false; this.busy = false;
this._queue = []; this._queue = [];

60
lib/http.js

@ -267,29 +267,34 @@ OutgoingMessage.prototype.sendHeaderLines = function (firstLine, headers) {
// in the case of response it is: "HTTP/1.1 200 OK\r\n" // in the case of response it is: "HTTP/1.1 200 OK\r\n"
var messageHeader = firstLine; var messageHeader = firstLine;
var field, value; var field, value;
for (var i in headers) {
if (headers[i] instanceof Array) {
field = headers[i][0];
value = headers[i][1];
} else {
if (!headers.hasOwnProperty(i)) continue;
field = i;
value = headers[i];
}
messageHeader += field + ": " + value + CRLF; if (headers) {
var keys = Object.keys(headers);
var isArray = (headers instanceof Array);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (isArray) {
field = headers[key][0];
value = headers[key][1];
} else {
field = key;
value = headers[key];
}
messageHeader += field + ": " + value + CRLF;
if (connectionExpression.test(field)) { if (connectionExpression.test(field)) {
sentConnectionHeader = true; sentConnectionHeader = true;
if (closeExpression.test(value)) this.closeOnFinish = true; if (closeExpression.test(value)) this.closeOnFinish = true;
} else if (transferEncodingExpression.test(field)) { } else if (transferEncodingExpression.test(field)) {
sentTransferEncodingHeader = true; sentTransferEncodingHeader = true;
if (chunkExpression.test(value)) this.chunkedEncoding = true; if (chunkExpression.test(value)) this.chunkedEncoding = true;
} else if (contentLengthExpression.test(field)) { } else if (contentLengthExpression.test(field)) {
sentContentLengthHeader = true; sentContentLengthHeader = true;
}
} }
} }
@ -696,10 +701,21 @@ exports.cat = function (url, encoding_, headers_) {
var url = require("url").parse(url); var url = require("url").parse(url);
var hasHost = false; var hasHost = false;
for (var i in headers) { if (headers instanceof Array) {
if (i.toLowerCase() === "host") { for (var i = 0, l = headers.length; i < l; i++) {
hasHost = true; if (headers[i][0].toLowerCase() === 'host') {
break; hasHost = true;
break;
}
}
} else if (typeof headers === "Object") {
var keys = Object.keys(headers);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (key.toLowerCase() == 'host') {
hasHost = true;
break;
}
} }
} }
if (!hasHost) headers["Host"] = url.hostname; if (!hasHost) headers["Host"] = url.hostname;

37
lib/ini.js

@ -42,24 +42,35 @@ function safe (val) {
return (val+"").replace(/[\n\r]+/g, " "); return (val+"").replace(/[\n\r]+/g, " ");
} }
// ForEaches over an object. The only thing faster is to inline this function.
function objectEach(obj, fn, thisObj) {
var keys, key, i, length;
keys = Object.keys(obj);
length = keys.length;
for (i = 0; i < length; i++) {
key = keys[i];
fn.call(thisObj, obj[key], key, obj);
}
}
exports.stringify = function (obj) { exports.stringify = function (obj) {
// if the obj has a "-" section, then do that first. // if the obj has a "-" section, then do that first.
var ini = ""; var ini = [];
if ("-" in obj) { if ("-" in obj) {
for (var key in obj["-"]) { objectEach(obj["-"], function (value, key) {
ini += safe(key)+" = "+safe(obj["-"][key])+"\n"; ini[ini.length] = safe(key) + " = " + safe(value) + "\n";
} });
} }
for (var section in obj) if (section !== "-") { objectEach(obj, function (section, name) {
ini += "[" + safe(section) + "]\n"; if (name === "-") return;
for (var key in obj[section]) { ini[ini.length] = "[" + safe(name) + "]\n";
objectEach(section, function (value, key) {
ini += safe(key) + ((obj[section][key] === true) ini[ini.length] = safe(key) + ((value === true)
? "\n" ? "\n"
: " = "+safe(obj[section][key])+"\n"); : " = "+safe(value)+"\n");
} });
} });
return ini; return ini.join("");
}; };
exports.encode = exports.stringify; exports.encode = exports.stringify;

18
lib/querystring.js

@ -55,9 +55,11 @@ QueryString.stringify = function (obj, sep, eq, name) {
var s = []; var s = [];
var begin = name ? name + '[' : ''; var begin = name ? name + '[' : '';
var end = name ? ']' : ''; var end = name ? ']' : '';
for (var i in obj) if (obj.hasOwnProperty(i)) { var keys = Object.keys(obj);
var n = begin + i + end; for (var i = 0, l = keys.length; i < l; i++) {
s.push(QueryString.stringify(obj[i], sep, eq, n)); var key = keys[i];
var n = begin + key + end;
s.push(QueryString.stringify(obj[key], sep, eq, n));
} }
stack.pop(); stack.pop();
@ -138,10 +140,14 @@ function mergeParams (params, addition) {
}; };
// Merge two *objects* together. If this is called, we've already ruled // Merge two *objects* together. If this is called, we've already ruled
// out the simple cases, and need to do the for-in business. // out the simple cases, and need to do a loop.
function mergeObjects (params, addition) { function mergeObjects (params, addition) {
for (var i in addition) if (i && addition.hasOwnProperty(i)) { var keys = Object.keys(addition);
params[i] = mergeParams(params[i], addition[i]); for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (key) {
params[key] = mergeParams(params[key], addition[key]);
}
} }
return params; return params;
}; };

6
lib/url.js

@ -56,7 +56,11 @@ function urlParse (url, parseQueryString) {
// pull out the auth and port. // pull out the auth and port.
var p = parseHost(out.host); var p = parseHost(out.host);
for (var i in p) out[i] = p[i]; var keys = Object.keys(p);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
out[key] = p[key];
}
// we've indicated that there is a hostname, so even if it's empty, it has to be present. // we've indicated that there is a hostname, so even if it's empty, it has to be present.
out.hostname = out.hostname || ""; out.hostname = out.hostname || "";
} }

8
src/node.js

@ -462,7 +462,9 @@ function findModulePath (id, dirs, callback) {
]; ];
var ext; var ext;
for (ext in extensionCache) { var extensions = Object.keys(extensionCache);
for (var i = 0, l = extensions.length; i < l; i++) {
var ext = extensions[i];
locations.push(path.join(dir, id + ext)); locations.push(path.join(dir, id + ext));
locations.push(path.join(dir, id, 'index' + ext)); locations.push(path.join(dir, id, 'index' + ext));
} }
@ -504,7 +506,9 @@ function resolveModulePath(request, parent) {
debug("RELATIVE: requested:" + request + " set ID to: "+id+" from "+parent.id); debug("RELATIVE: requested:" + request + " set ID to: "+id+" from "+parent.id);
var exts = ['js', 'node'], ext; var exts = ['js', 'node'], ext;
for (ext in extensionCache) { var extensions = Object.keys(extensionCache);
for (var i = 0, l = extensions.length; i < l; i++) {
var ext = extensions[i];
exts.push(ext.slice(1)); exts.push(ext.slice(1));
} }

Loading…
Cancel
Save