Skip to content

fix: O(n²) jPath degradation when tag names contain dots#794

Open
dollebrolle wants to merge 1 commit intoNaturalIntelligence:masterfrom
dollebrolle:dotted-tagname-jpath-793
Open

fix: O(n²) jPath degradation when tag names contain dots#794
dollebrolle wants to merge 1 commit intoNaturalIntelligence:masterfrom
dollebrolle:dotted-tagname-jpath-793

Conversation

@dollebrolle
Copy link

Purpose / Goal

Fixes #793

The jPath variable in OrderedObjParser.js uses . as a separator to track tag hierarchy (e.g., root.parent.child). When closing tags, lastIndexOf(".") is used to pop back one level. However, when tag names themselves contain dots (e.g., <applink.ios_url>), lastIndexOf(".") finds the dot within the tag name instead of the hierarchy separator, causing incorrect truncation.

This leaves residue in the jPath string that accumulates with every dotted tag processed, resulting in O(n²) performance degradation.

Changes

Replaced all 6 lastIndexOf(".")-based jPath truncations with a popTagFromJPath(jPath, tagName) helper that uses the known tag name length to compute the exact substring position. Since jPath is always built by appending "." + tagName, we can reverse it precisely without searching for dots.

Additionally, lastTagName (used for unpaired tag detection) is now read from currentNode.tagname instead of being parsed from jPath via lastIndexOf(".").

Benchmark

Parser              | Before fix | After fix
fxp v3 (baseline)   | 75,283     | 75,802
fxp                  | 49,306     | 52,690
fxp - preserve order | 53,841     | 56,949
xmlbuilder2          | 18,397     | 18,469
xml2js               | 22,083     | 21,319

No performance regression for standard XML parsing.

Type

  • Bug Fix
  • Refactoring / Technology upgrade
  • New Feature

Note : Please ensure that you've read contribution guidelines before raising this PR. If your PR is in progress, please prepend [WIP] in PR title. Your PR will be reviewed when [WIP] will be removed from the PR title.

Bookmark this repository for further updates.

…contain dots

Replace lastIndexOf(".")-based jPath truncation with a popTagFromJPath
helper that uses the known tag name length, correctly handling dots
within XML tag names.
@amitguptagwl
Copy link
Member

Thanks for your changes. I'm planning to handle it differently. Working on this. will update in few days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

O(n²) performance degradation when tag names contain dots

2 participants