From be912d4af91ba6448853ffceae7e3b761ba4db66 Mon Sep 17 00:00:00 2001 From: g4lb Date: Wed, 18 Nov 2026 11:24:04 +0200 Subject: [PATCH] perf(node): build toString output by concatenation --- src/selectors/container.js | 13 +++++++++++-- src/selectors/node.js | 4 +++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/selectors/container.js b/src/selectors/container.js index efd1a4b..4779437 100644 --- a/src/selectors/container.js +++ b/src/selectors/container.js @@ -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)`, diff --git a/src/selectors/node.js b/src/selectors/node.js index 7363c60..2e8759d 100644 --- a/src/selectors/node.js +++ b/src/selectors/node.js @@ -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