Skip to content

Optimize Parser.raw to batch raws.before/between extraction in a single token scan #73

@alanzabihi

Description

@alanzabihi

Hypothesis

During AST construction, the parser processes tokens in several methods (decl, rule, atrule, other) that each independently handle whitespace/comment tokens. For a typical declaration, the token flow involves:

  1. Collect tokens in other() until reaching a boundary
  2. Call spacesAndCommentsFromEnd() — scans tokens backwards
  3. Call spacesAndCommentsFromStart() — scans tokens forwards
  4. Call raw() to extract the value — iterates tokens again

These three passes over the same token array are redundant. A single forward scan could extract before-spaces, the prop, between-spaces, the value, and after-spaces in one pass, populating all raws fields simultaneously.

Implement a single-pass token processor for the common declaration case:

// Single pass: scan tokens[0..n], partition into:
// before_spaces | prop_tokens | between_spaces | value_tokens | after_spaces
// Set all raws in one go

Editable surface

  • lib/parser.js — single-pass token processing for common declaration/rule patterns

What's different from prior work

Expected impact

METRIC_A improvement of 2-4ms (large file, thousands of declarations). METRIC_B improvement of 2-4ms. Combined 3-5ms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions