Improve performance on hot paths by using for loops

This commit is contained in:
Dan Abramov 2015-01-09 16:51:34 +03:00
parent 1231dc6cef
commit 55e01afd0d
3 changed files with 27 additions and 24 deletions

View File

@ -77,10 +77,9 @@ Buffer.prototype.newline = function (i, removeLast) {
if (this.endsWith("{\n")) i--;
if (this.endsWith(util.repeat(i, "\n"))) return;
var self = this;
_.times(i, function () {
self.newline(null, removeLast);
});
for (var j = 0; j < i; j++) {
this.newline(null, removeLast);
}
return;
}
@ -121,7 +120,11 @@ Buffer.prototype.endsWith = function (str) {
Buffer.prototype.isLast = function (cha, trimRight) {
var buf = this.buf;
if (trimRight) buf = util.trimRight(buf);
var last = _.last(buf);
var chars = [].concat(cha);
return _.contains(chars, _.last(buf));
if (_.isArray(cha)) {
return _.contains(cha, last);
} else {
return cha === last;
}
};

View File

@ -1,33 +1,27 @@
module.exports = Position;
var _ = require("lodash");
function Position() {
this.line = 1;
this.column = 0;
}
Position.prototype.push = function (str) {
var self = this;
_.each(str, function (cha) {
if (cha === "\n") {
self.line++;
self.column = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] === "\n") {
this.line++;
this.column = 0;
} else {
self.column++;
this.column++;
}
}
});
};
Position.prototype.unshift = function (str) {
var self = this;
_.each(str, function (cha) {
if (cha === "\n") {
self.line--;
for (var i = 0; i < str.length; i++) {
if (str[i] === "\n") {
this.line--;
} else {
self.column--;
this.column--;
}
}
});
};

View File

@ -188,7 +188,13 @@ exports.codeFrame = function (lines, lineNumber, colNumber) {
exports.repeat = function (width, cha) {
cha = cha || " ";
return new Array(width + 1).join(cha);
var result = "";
for (var i = 0; i < width; i++) {
result += cha;
}
return result;
};
exports.normaliseAst = function (ast, comments, tokens) {