Skip to content

Conversation

@macieklamberski
Copy link

Purpose / Goal

Hi! I'm creator of a JS feed parsing library Feedsmith which heavily relies on stopNodes functionality.

This PR optimizes the stopNodes feature 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 stopNodes list. The old way looped through the entire stopNodes array 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:

  • Split them into two quick-lookup groups (using JavaScript Sets)
  • During parsing, just check "is this tag in the list?" instead of looping

Results

I prepared a benchmark showing comparing the original vs optimized library: https://github.com/macieklamberski/fast-xml-parser-benchmarks.

=======================================================================
Speedup (Original / Optimized)
=======================================================================

StopNodes \ Items |         10 |         20 |         50 |        100 |
                0 |      1.01x |      1.01x |      1.01x |      1.01x |
               10 |      1.05x |      1.06x |      1.09x |      1.11x |
               25 |      1.04x |      1.13x |      1.18x |      1.20x |
               50 |      1.12x |      1.21x |      1.31x |      1.39x |
              100 |      1.17x |      1.29x |      1.41x |      1.48x |

Type

  • Bug Fix
  • Refactoring / Technology upgrade
  • New Feature

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.

1 participant