perf: Use Set for stopNodes lookup O(1) instead of array iteration O(n) #769
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose / Goal
Hi! I'm creator of a JS feed parsing library Feedsmith which heavily relies on
stopNodesfunctionality.This PR optimizes the
stopNodesfeature by replacing O(n) array iteration with O(1) Set-based lookups, resulting in 5-48% faster parsing when using stopNodes.Problem
Every time the parser encounters an opening XML tag, it checks if that tag is in the
stopNodeslist. The old way looped through the entirestopNodesarray every single time, which got slower as more stopNodes were set in options.Solution
Instead of checking the stopNodes list over and over during parsing, this MR suggests organizing them once when the parser starts:
Results
I prepared a benchmark showing comparing the original vs optimized library: https://github.com/macieklamberski/fast-xml-parser-benchmarks.
Type