Scenario

Performance Foundations: Complexity, Memory, and Thrashing

Managing exponential slowdowns in large documents and browser-specific engine thrashing.

performance
Scenario ID
scenario-performance-foundations

Details

Overview

Contenteditable editors degrade exponentially as content grows. Scaling to 10k+ characters requires moving away from native DOM traversal towards structured indexing and virtualization.

Technical Bottlenecks

1. CSS RuleSet Invalidation (2025 Regression)

Mobile Safari 18.6 exhibits $O(n^2)$ complexity when joining RuleSets for attribute selectors.

  • Result: Typing inside an editor that triggers class changes (e.g., for syntax highlighting) can hang the UI for seconds on pages with large stylesheets.

2. Selection API Exponential Lag

Operations like window.getSelection().addRange() become progressively slower as the DOM depth increases.

  • Cause: The engine performs linear node searches to resolve offsets for every selection change.

3. Memory Leaks in Detached Nodes

Pasting large blocks or performing rapid UNDO operations can leave thousands of detached nodes in memory if event listeners aren’t strictly managed.

Optimization Blueprint

Structured Indexing

Maintain a flat array of ‘text node’ metadata to perform O(1) character-to-node lookups, bypassing expensive TreeWalkers.

Rule Flattening

Avoid complex attribute selectors like [class*="editor-"] globally. Use unique class names to prevent the $O(n^2)$ invalidation path in WebKit.

Scenario flow

Visual view of how this scenario connects to its concrete cases and environments. Nodes can be dragged and clicked.

React Flow mini map

Variants

Each row is a concrete case for this scenario, with a dedicated document and playground.

Case OS Device Browser Keyboard Status
ce-0578 macOS 15.0 Desktop Any Safari 18.6 Any confirmed

Cases

Open a case to see the detailed description and its dedicated playground.

Related Scenarios

Other scenarios that share similar tags or category.

Tags: performance

CSS will-change may improve or degrade contenteditable performance

When a contenteditable element has CSS will-change property set, performance may be affected. In some cases, it may improve performance by hinting the browser about upcoming changes. In other cases, it may degrade performance by creating unnecessary layers.

1 case
Tags: performance

MutationObserver may interfere with contenteditable editing

When a MutationObserver is attached to a contenteditable element or its parent, the observer callbacks may interfere with editing performance. Frequent DOM mutations during typing can trigger many observer callbacks, causing lag or jank.

1 case
Tags: performance

Virtual scrolling libraries interfere with contenteditable selection

When a contenteditable element is used with virtual scrolling libraries (e.g., for large documents), the virtual scrolling mechanism may interfere with text selection and caret positioning. The selection may be lost when elements are removed from the DOM during scrolling.

1 case

Comments & Discussion

Have questions, suggestions, or want to share your experience? Join the discussion below.