From 55e01afd0dc110469dbf928b83c0279be7a1b8f7 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 9 Jan 2015 16:51:34 +0300 Subject: [PATCH] Improve performance on hot paths by using for loops --- lib/6to5/generation/buffer.js | 15 +++++++++------ lib/6to5/generation/position.js | 28 +++++++++++----------------- lib/6to5/util.js | 8 +++++++- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/6to5/generation/buffer.js b/lib/6to5/generation/buffer.js index afd9c4dd23..e40d4787f7 100644 --- a/lib/6to5/generation/buffer.js +++ b/lib/6to5/generation/buffer.js @@ -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; + } }; diff --git a/lib/6to5/generation/position.js b/lib/6to5/generation/position.js index bd363400f3..cae67ca356 100644 --- a/lib/6to5/generation/position.js +++ b/lib/6to5/generation/position.js @@ -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--; } - }); + } }; diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 7b5ffb7cca..ee5c332ff7 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -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) {