Skip to content

Commit f5c16de

Browse files
committed
Reuse temporary objects instead of returning a new one for each call to decode
1 parent c217ce1 commit f5c16de

3 files changed

Lines changed: 12 additions & 15 deletions

File tree

lib/source-map/base64-vlq.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ define(function (require, exports, module) {
115115

116116
/**
117117
* Decodes the next base 64 VLQ value from the given string and returns the
118-
* value and the rest of the string.
118+
* value and the rest of the string via the out parameter.
119119
*/
120-
exports.decode = function base64VLQ_decode(aStr) {
120+
exports.decode = function base64VLQ_decode(aStr, aOutParam) {
121121
var i = 0;
122122
var strLen = aStr.length;
123123
var result = 0;
@@ -135,10 +135,8 @@ define(function (require, exports, module) {
135135
shift += VLQ_BASE_SHIFT;
136136
} while (continuation);
137137

138-
return {
139-
value: fromVLQSigned(result),
140-
rest: aStr.slice(i)
141-
};
138+
aOutParam.value = fromVLQSigned(result);
139+
aOutParam.rest = aStr.slice(i);
142140
};
143141

144142
});

lib/source-map/source-map-consumer.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ define(function (require, exports, module) {
197197
var previousSource = 0;
198198
var previousName = 0;
199199
var str = aStr;
200+
var temp = {};
200201
var mapping;
201-
var temp;
202202

203203
while (str.length > 0) {
204204
if (str.charAt(0) === ';') {
@@ -214,14 +214,14 @@ define(function (require, exports, module) {
214214
mapping.generatedLine = generatedLine;
215215

216216
// Generated column.
217-
temp = base64VLQ.decode(str);
217+
base64VLQ.decode(str, temp);
218218
mapping.generatedColumn = previousGeneratedColumn + temp.value;
219219
previousGeneratedColumn = mapping.generatedColumn;
220220
str = temp.rest;
221221

222222
if (str.length > 0 && !this._nextCharIsMappingSeparator(str)) {
223223
// Original source.
224-
temp = base64VLQ.decode(str);
224+
base64VLQ.decode(str, temp);
225225
mapping.source = this._sources.at(previousSource + temp.value);
226226
previousSource += temp.value;
227227
str = temp.rest;
@@ -230,7 +230,7 @@ define(function (require, exports, module) {
230230
}
231231

232232
// Original line.
233-
temp = base64VLQ.decode(str);
233+
base64VLQ.decode(str, temp);
234234
mapping.originalLine = previousOriginalLine + temp.value;
235235
previousOriginalLine = mapping.originalLine;
236236
// Lines are stored 0-based
@@ -241,14 +241,14 @@ define(function (require, exports, module) {
241241
}
242242

243243
// Original column.
244-
temp = base64VLQ.decode(str);
244+
base64VLQ.decode(str, temp);
245245
mapping.originalColumn = previousOriginalColumn + temp.value;
246246
previousOriginalColumn = mapping.originalColumn;
247247
str = temp.rest;
248248

249249
if (str.length > 0 && !this._nextCharIsMappingSeparator(str)) {
250250
// Original name.
251-
temp = base64VLQ.decode(str);
251+
base64VLQ.decode(str, temp);
252252
mapping.name = this._names.at(previousName + temp.value);
253253
previousName += temp.value;
254254
str = temp.rest;

test/source-map/test-base64-vlq.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ define(function (require, exports, module) {
1212
var base64VLQ = require('../../lib/source-map/base64-vlq');
1313

1414
exports['test normal encoding and decoding'] = function (assert, util) {
15-
var result;
15+
var result = {};
1616
for (var i = -255; i < 256; i++) {
17-
result = base64VLQ.decode(base64VLQ.encode(i));
18-
assert.ok(result);
17+
base64VLQ.decode(base64VLQ.encode(i), result);
1918
assert.equal(result.value, i);
2019
assert.equal(result.rest, "");
2120
}

0 commit comments

Comments
 (0)