Merge pull request #432 from gaearon/perf-stable

Replace _.each and for-in with for loop in hot paths
This commit is contained in:
Sebastian McKenzie
2015-01-10 21:49:37 +11:00
6 changed files with 65 additions and 42 deletions

View File

@@ -6,14 +6,19 @@ var t = require("../../types");
var _ = require("lodash");
var find = function (obj, node, parent) {
if (!obj) return;
var result;
_.each(obj, function (fn, type) {
var types = Object.keys(obj);
for (var i = 0; i < types.length; i++) {
var type = types[i];
if (t["is" + type](node)) {
var fn = obj[type];
result = fn(node, parent);
if (result != null) return false;
if (result != null) break;
}
});
}
return result;
};
@@ -99,7 +104,13 @@ _.each(Node.prototype, function (fn, key) {
Node[key] = function (node, parent) {
var n = new Node(node, parent);
var args = _.toArray(arguments).slice(2);
// Avoid leaking arguments to prevent deoptimization
var skipCount = 2;
var args = new Array(arguments.length - skipCount);
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i + 2];
}
return n[key].apply(n, args);
};
});