Skip to content

perf(swc_ts_fast_strip): reduce repeated binary-search token lookups in TsStrip #11657

@kdy1

Description

@kdy1

Summary

TsStrip performs many token-position lookups via binary search, which likely adds avoidable log-factor overhead.

Evidence

  • Core lookup helpers use binary search every time:
    • crates/swc_ts_fast_strip/src/lib.rs:627-633 (get_next_token_index)
    • crates/swc_ts_fast_strip/src/lib.rs:639-644 (get_prev_token_index)
  • These lookups are called from many visitor paths (visit_arrow_expr, visit_class_prop, visit_params, visit_getter_prop, etc.).

Why this is a performance risk

TsStrip does a lot of local token queries while walking the AST. Repeated binary_search_by_key calls can add up to O(k log n) token-lookup cost (k = number of strip/fix operations).

Suggested direction

  • Introduce an index cursor in TsStrip and advance it as traversal moves forward in source order (amortized near O(1) local lookups).
  • Alternatively, precompute a compact position->token-index table for the span boundaries used by stripping logic.

Benchmark idea

Use the existing typescript/fast-strip benchmark and compare time spent in token lookup helpers before/after cursorization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions