From 9d5e357a210e88381bc97b5d73389b3835eeffed Mon Sep 17 00:00:00 2001 From: Louis-Dominique Dubeau Date: Tue, 21 Aug 2018 20:19:40 -0400 Subject: [PATCH] perf: add emitNodes to skip checking text buffer more than needed --- lib/saxes.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/saxes.js b/lib/saxes.js index 976c74bd..a7aa22d0 100644 --- a/lib/saxes.js +++ b/lib/saxes.js @@ -1567,6 +1567,25 @@ class SaxesParser { this[nodeType](data); } + /** + * Emit any buffered text. Then emit the specified node types. + * + * @param {string} nodeTypeA The node type to emit. + * + * @param {string} nodeTypeB The node type to emit. + * + * @param {string} data The data associated with the node type. + * + * @private + */ + emitNodes(nodeTypeA, nodeTypeB, data) { + if (this.textNode) { + this.closeText(); + } + this[nodeTypeA](data); + this[nodeTypeB](data); + } + resolve(prefix) { let uri = this.tag.ns[prefix]; if (uri !== undefined) { @@ -1713,16 +1732,16 @@ class SaxesParser { this.fail("documents may contain only one root."); } this.sawRoot = true; - this.emitNode("onopentag", tag); const { tags } = this; if (selfClosing) { - this.emitNode("onclosetag", tag); + this.emitNodes("onopentag", "onclosetag", tag); this.tag = tags[tags.length - 1]; if (!this.tag) { this.closedRoot = true; } } else { + this.emitNode("onopentag", tag); this.inRoot = true; tags.push(tag); }