Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/selectors/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,17 @@ export default class Container extends Node {
}

_stringify(options, depth, max) {
const str = this.map((child) => this._stringifyChild(child, options, depth, max)).join("");
return [this.rawSpaceBefore, str, this.rawSpaceAfter].join("");
// Build the string in one pass instead of mapping to an array and joining.
// `join` turned a null or undefined part into "", so that is kept here.
const nodes = this.nodes;
let str = "";
for (let i = 0; i < nodes.length; i++) {
const part = this._stringifyChild(nodes[i], options, depth, max);
if (part !== undefined && part !== null) {
str += part;
}
}
return this.rawSpaceBefore + str + this.rawSpaceAfter;
}

// Serialize a child node. Historically `toString` used `this.map(String)`,
Expand Down
4 changes: 3 additions & 1 deletion src/selectors/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ export default class Node {
}

toString() {
return [this.rawSpaceBefore, this.valueToString(), this.rawSpaceAfter].join("");
// All three parts are strings, so plain concatenation gives the same result
// as the array `join` without allocating an array per node.
return this.rawSpaceBefore + this.valueToString() + this.rawSpaceAfter;
}

// Internal recursion entry point used by Container serialization. Leaf
Expand Down
Loading