Changeset: Use string concatenation instead of array join

People report that string concatenation is faster. Also, I think it's
more readable.
pull/5268/head
Richard Hansen 2021-10-11 04:31:28 -04:00
parent 097f2623c6
commit 0ae8fb1441
1 changed files with 12 additions and 22 deletions

View File

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