Skip to content

Combine patterns into a single regex for better performance #7

Description

@srbdev

Problem

Currently, each line is scanned multiple times—once per pattern—using indexOf:

Object.entries(patterns).forEach(([key, value]) => {
    let index = 0;
    while ((index = line.indexOf(value.pattern, index)) \!== -1) {
        // ...
    }
});

With 9 patterns, this means 9 passes per line.

Proposed Solution

Combine all patterns into a single regex with capture groups:

const combinedRegex = /\b(TODO|FIXME|FIX|HACK|NOTE|INFO|PERF|WARN|WARNING):/gi;
let match;
while ((match = combinedRegex.exec(line)) \!== null) {
    const pattern = match[1].toUpperCase();
    // look up decoration type by pattern and apply
}

Benefits

  • Single pass per line instead of 9
  • Enables case-insensitive matching (addresses Support case-insensitive pattern matching #4)
  • Easier to add word boundary checks (\b) to avoid partial matches
  • Foundation for user-configurable patterns

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions