Replace _.each and for-in with for loop in hot paths

This commit is contained in:
Dan Abramov 2015-01-09 16:00:53 +03:00
parent 89148e6029
commit 1231dc6cef
2 changed files with 20 additions and 12 deletions

View File

@ -11,15 +11,18 @@ Whitespace.prototype.getNewlinesBefore = function (node) {
var startToken;
var endToken;
var tokens = this.tokens;
var token;
for (var i = 0; i < tokens.length; i++) {
token = tokens[i];
_.each(tokens, function (token, i) {
// this is the token this node starts with
if (node.start === token.start) {
startToken = tokens[i - 1];
endToken = token;
return false;
break;
}
});
}
return this.getNewlinesBetween(startToken, endToken);
};
@ -28,15 +31,18 @@ Whitespace.prototype.getNewlinesAfter = function (node) {
var startToken;
var endToken;
var tokens = this.tokens;
var token;
for (var i = 0; i < tokens.length; i++) {
token = tokens[i];
_.each(tokens, function (token, i) {
// this is the token this node ends with
if (node.end === token.end) {
startToken = token;
endToken = tokens[i + 1];
return false;
break;
}
});
}
if (endToken.type.type === "eof") {
return 1;

View File

@ -8,11 +8,13 @@ function traverse(parent, opts, scope) {
// falsy node
if (!parent) return;
var i, j;
// array of nodes
if (_.isArray(parent)) {
_.each(parent, function (node) {
traverse(node, opts, scope);
});
for (i = 0; i < parent.length; i++) {
traverse(parent[i], opts, scope);
}
return;
}
@ -24,7 +26,7 @@ function traverse(parent, opts, scope) {
var stopped = false;
for (var i in keys) {
for (i = 0; i < keys.length; i++) {
var key = keys[i];
var nodes = parent[key];
if (!nodes) continue;
@ -108,8 +110,8 @@ function traverse(parent, opts, scope) {
};
if (_.isArray(nodes)) {
for (i in nodes) {
handle(nodes, i);
for (j = 0; j < nodes.length; j++) {
handle(nodes, j);
if (stopped) return;
}