From c217ce12ae320ef23a89bb444d484bfbc4ae2172 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 30 Sep 2014 10:33:28 +0100 Subject: [PATCH 1/2] Don't use a RegExp for finding mapping separators in SourceMapConsumer.prototype._parseMappings --- lib/source-map/source-map-consumer.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/source-map/source-map-consumer.js b/lib/source-map/source-map-consumer.js index eec18bd3..160d39a0 100644 --- a/lib/source-map/source-map-consumer.js +++ b/lib/source-map/source-map-consumer.js @@ -177,6 +177,12 @@ define(function (require, exports, module) { } }); + SourceMapConsumer.prototype._nextCharIsMappingSeparator = + function SourceMapConsumer_nextCharIsMappingSeparator(aStr) { + var c = aStr.charAt(0); + return c === ";" || c === ","; + }; + /** * Parse the mappings in a string in to a data structure which we can easily * query (the ordered arrays in the `this.__generatedMappings` and @@ -190,7 +196,6 @@ define(function (require, exports, module) { var previousOriginalColumn = 0; var previousSource = 0; var previousName = 0; - var mappingSeparator = /^[,;]/; var str = aStr; var mapping; var temp; @@ -214,13 +219,13 @@ define(function (require, exports, module) { previousGeneratedColumn = mapping.generatedColumn; str = temp.rest; - if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) { + if (str.length > 0 && !this._nextCharIsMappingSeparator(str)) { // Original source. temp = base64VLQ.decode(str); mapping.source = this._sources.at(previousSource + temp.value); previousSource += temp.value; str = temp.rest; - if (str.length === 0 || mappingSeparator.test(str.charAt(0))) { + if (str.length === 0 || this._nextCharIsMappingSeparator(str)) { throw new Error('Found a source, but no line and column'); } @@ -231,7 +236,7 @@ define(function (require, exports, module) { // Lines are stored 0-based mapping.originalLine += 1; str = temp.rest; - if (str.length === 0 || mappingSeparator.test(str.charAt(0))) { + if (str.length === 0 || this._nextCharIsMappingSeparator(str)) { throw new Error('Found a source and line, but no column'); } @@ -241,7 +246,7 @@ define(function (require, exports, module) { previousOriginalColumn = mapping.originalColumn; str = temp.rest; - if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) { + if (str.length > 0 && !this._nextCharIsMappingSeparator(str)) { // Original name. temp = base64VLQ.decode(str); mapping.name = this._names.at(previousName + temp.value); From 4d067382a18d26db74e55502ef573435fcb2446f Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 30 Sep 2014 15:47:25 +0100 Subject: [PATCH 2/2] Reuse temporary objects instead of returning a new one for each call to decode --- lib/source-map/base64-vlq.js | 10 ++++------ lib/source-map/source-map-consumer.js | 12 ++++++------ test/source-map/test-base64-vlq.js | 5 ++--- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/source-map/base64-vlq.js b/lib/source-map/base64-vlq.js index 1b67bb37..b4ff136b 100644 --- a/lib/source-map/base64-vlq.js +++ b/lib/source-map/base64-vlq.js @@ -115,9 +115,9 @@ define(function (require, exports, module) { /** * Decodes the next base 64 VLQ value from the given string and returns the - * value and the rest of the string. + * value and the rest of the string via the out parameter. */ - exports.decode = function base64VLQ_decode(aStr) { + exports.decode = function base64VLQ_decode(aStr, aOutParam) { var i = 0; var strLen = aStr.length; var result = 0; @@ -135,10 +135,8 @@ define(function (require, exports, module) { shift += VLQ_BASE_SHIFT; } while (continuation); - return { - value: fromVLQSigned(result), - rest: aStr.slice(i) - }; + aOutParam.value = fromVLQSigned(result); + aOutParam.rest = aStr.slice(i); }; }); diff --git a/lib/source-map/source-map-consumer.js b/lib/source-map/source-map-consumer.js index 160d39a0..ea643bf0 100644 --- a/lib/source-map/source-map-consumer.js +++ b/lib/source-map/source-map-consumer.js @@ -197,8 +197,8 @@ define(function (require, exports, module) { var previousSource = 0; var previousName = 0; var str = aStr; + var temp = {}; var mapping; - var temp; while (str.length > 0) { if (str.charAt(0) === ';') { @@ -214,14 +214,14 @@ define(function (require, exports, module) { mapping.generatedLine = generatedLine; // Generated column. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.generatedColumn = previousGeneratedColumn + temp.value; previousGeneratedColumn = mapping.generatedColumn; str = temp.rest; if (str.length > 0 && !this._nextCharIsMappingSeparator(str)) { // Original source. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.source = this._sources.at(previousSource + temp.value); previousSource += temp.value; str = temp.rest; @@ -230,7 +230,7 @@ define(function (require, exports, module) { } // Original line. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.originalLine = previousOriginalLine + temp.value; previousOriginalLine = mapping.originalLine; // Lines are stored 0-based @@ -241,14 +241,14 @@ define(function (require, exports, module) { } // Original column. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.originalColumn = previousOriginalColumn + temp.value; previousOriginalColumn = mapping.originalColumn; str = temp.rest; if (str.length > 0 && !this._nextCharIsMappingSeparator(str)) { // Original name. - temp = base64VLQ.decode(str); + base64VLQ.decode(str, temp); mapping.name = this._names.at(previousName + temp.value); previousName += temp.value; str = temp.rest; diff --git a/test/source-map/test-base64-vlq.js b/test/source-map/test-base64-vlq.js index 653a874e..6fd0d99f 100644 --- a/test/source-map/test-base64-vlq.js +++ b/test/source-map/test-base64-vlq.js @@ -12,10 +12,9 @@ define(function (require, exports, module) { var base64VLQ = require('../../lib/source-map/base64-vlq'); exports['test normal encoding and decoding'] = function (assert, util) { - var result; + var result = {}; for (var i = -255; i < 256; i++) { - result = base64VLQ.decode(base64VLQ.encode(i)); - assert.ok(result); + base64VLQ.decode(base64VLQ.encode(i), result); assert.equal(result.value, i); assert.equal(result.rest, ""); }