A few minor perf improvements

This commit is contained in:
Dan Abramov 2015-01-09 20:37:02 +03:00
parent 55e01afd0d
commit 5783973734
2 changed files with 19 additions and 7 deletions

View File

@ -61,7 +61,7 @@ Buffer.prototype.space = function () {
Buffer.prototype.removeLast = function (cha) {
if (!this.isLast(cha)) return;
this.buf = this.buf.slice(0, -1);
this.buf = this.buf.substr(0, this.buf.length - 1);
this.position.unshift(cha);
};
@ -114,13 +114,14 @@ Buffer.prototype._push = function (str) {
};
Buffer.prototype.endsWith = function (str) {
return this.buf.slice(-str.length) === str;
var d = this.buf.length - str.length;
return d >= 0 && this.buf.lastIndexOf(str) === d;
};
Buffer.prototype.isLast = function (cha, trimRight) {
var buf = this.buf;
if (trimRight) buf = util.trimRight(buf);
var last = _.last(buf);
var last = buf[buf.length - 1];
if (_.isArray(cha)) {
return _.contains(cha, last);

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);
};
});