Changeset: Use string concatenation instead of array join

People report that string concatenation is faster. Also, I think it's
more readable.
This commit is contained in:
Richard Hansen 2021-10-11 04:31:28 -04:00
parent 097f2623c6
commit 0ae8fb1441

View file

@ -482,24 +482,22 @@ exports.mergingOpAssembler = () => {
* @returns {OpAssembler} * @returns {OpAssembler}
*/ */
exports.opAssembler = () => { exports.opAssembler = () => {
const pieces = []; let serialized = '';
/** /**
* @param {Op} op - Operation to add. Ownership remains with the caller. * @param {Op} op - Operation to add. Ownership remains with the caller.
*/ */
const append = (op) => { const append = (op) => {
pieces.push(op.attribs); if (op.attribs != null) serialized += op.attribs;
if (op.lines) { if (op.lines) serialized += `|${exports.numToString(op.lines)}`;
pieces.push('|', exports.numToString(op.lines)); if (op.opcode != null) serialized += op.opcode;
} serialized += exports.numToString(op.chars);
pieces.push(op.opcode);
pieces.push(exports.numToString(op.chars));
}; };
const toString = () => pieces.join(''); const toString = () => serialized;
const clear = () => { const clear = () => {
pieces.length = 0; serialized = '';
}; };
return { return {
append, append,
@ -573,22 +571,14 @@ exports.stringIterator = (str) => {
/** /**
* @returns {StringAssembler} * @returns {StringAssembler}
*/ */
exports.stringAssembler = () => { exports.stringAssembler = () => ({
const pieces = []; _str: '',
/** /**
* @param {string} x - * @param {string} x -
*/ */
const append = (x) => { append(x) { this._str += String(x); },
pieces.push(String(x)); toString() { return this._str; },
}; });
const toString = () => pieces.join('');
return {
append,
toString,
};
};
/** /**
* @typedef {object} StringArrayLike * @typedef {object} StringArrayLike