Skip to content

[lit-html] removePart() leaks end marker comment, causing unbounded DOM accumulation under sustained churn #5298

Open

Description

@graynorton

Which package(s) are affected?

Lit Core (lit / lit-html / lit-element / reactive-element)

Description

removePart() in lit-html/directive-helpers.js only removes the part's start marker, leaving the end marker (<!---->) orphaned in the DOM. Parts created via insertPart() own two unique marker comments, and both must be removed.

Each call leaks one <!----> comment node. Under sustained churn, these accumulate without bound — a visible effect is @lit-labs/virtualizer scrolling, where items are continuously removed at one edge and added at the other. As the container fills with orphan comment nodes, per-operation cost (e.g. subsequent node.remove() calls) climbs steadily, causing observable scroll performance degradation over time.

The regression was introduced in #4975 ("Remove some redundant code from removePart()") and shipped in [email protected]. Prior to that PR, removePart walked sibling nodes up to (and including) the end marker, correctly removing both markers.

The existing test 'removePart removes the start marker' (added alongside #4975) does not catch this because it uses a directive at the end of a <div>, where the part's _$endNode is null. The bug only manifests when _$endNode is a real marker node, which is the case for parts created via insertPart().

Reproduction

Local tachometer measurements against lit-html/repeat (15 samples, Chromium, local build, A/B vs. patched build):

  • lit-html/repeat update: 595.28 ms → 63.08 ms (−89.4%)
  • lit-html/repeat-full remove: 54.90 ms → 0.23 ms (−99.6%)
  • reverse / shuffle / replace: −98.8% / −98.3% / −97.5%

A minimal programmatic repro (regression test) uses insertPart() to create a ChildPart with a non-null _$endNode, calls removePart(), and observes that the container's childNodes.length only drops by 1 instead of 2.

Workaround

I have not found a workaround at the application level. Downgrading to [email protected] avoids the regression.

Is this a regression?

Yes. This used to work, but now it doesn't.

Affected versions

Failing in [email protected] and later. Last known working version: [email protected]. Regression introduced in #4975.

Browser/OS/Node environment

Reproduced in Chromium, Firefox, and Webkit via the lit-html test suite.

  • OS: macOS 15 (Darwin 25.0.0)
  • Node: matches repo .nvmrc

A fix and a regression test are ready in a branch; PR to follow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions