Skip to content

Commit 10c21fd

Browse files
committed
Changeset: Use string concatenation instead of array join
People report that string concatenation is faster. Also, I think it's more readable.
1 parent d7c5180 commit 10c21fd

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

src/static/js/Changeset.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -482,24 +482,22 @@ exports.mergingOpAssembler = () => {
482482
* @returns {OpAssembler}
483483
*/
484484
exports.opAssembler = () => {
485-
const pieces = [];
485+
let serialized = '';
486486

487487
/**
488488
* @param {Op} op - Operation to add. Ownership remains with the caller.
489489
*/
490490
const append = (op) => {
491-
pieces.push(op.attribs);
492-
if (op.lines) {
493-
pieces.push('|', exports.numToString(op.lines));
494-
}
495-
pieces.push(op.opcode);
496-
pieces.push(exports.numToString(op.chars));
491+
if (op.attribs != null) serialized += op.attribs;
492+
if (op.lines) serialized += `|${exports.numToString(op.lines)}`;
493+
if (op.opcode != null) serialized += op.opcode;
494+
serialized += exports.numToString(op.chars);
497495
};
498496

499-
const toString = () => pieces.join('');
497+
const toString = () => serialized;
500498

501499
const clear = () => {
502-
pieces.length = 0;
500+
serialized = '';
503501
};
504502
return {
505503
append,
@@ -573,22 +571,14 @@ exports.stringIterator = (str) => {
573571
/**
574572
* @returns {StringAssembler}
575573
*/
576-
exports.stringAssembler = () => {
577-
const pieces = [];
578-
574+
exports.stringAssembler = () => ({
575+
_str: '',
579576
/**
580577
* @param {string} x -
581578
*/
582-
const append = (x) => {
583-
pieces.push(String(x));
584-
};
585-
586-
const toString = () => pieces.join('');
587-
return {
588-
append,
589-
toString,
590-
};
591-
};
579+
append(x) { this._str += String(x); },
580+
toString() { return this._str; },
581+
});
592582

593583
/**
594584
* @typedef {object} StringArrayLike

0 commit comments

Comments
 (0)