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

View File

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

View File

@ -188,7 +188,13 @@ exports.codeFrame = function (lines, lineNumber, colNumber) {
exports.repeat = function (width, cha) { exports.repeat = function (width, cha) {
cha = 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) { exports.normaliseAst = function (ast, comments, tokens) {