lukehoban / lukehoban/browser

Performance: Classes() parses class string on every call

Open
#73 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance
Dominant language
Go
Stars
1
Forks
2
PR merge metrics
No merged PRs in 30d

Description

## Description

The `dom.Node.Classes()` method parses the class attribute string every time it's called, with no caching.

## Location

- `dom/node.go:85-101` - `Classes()` method
- `style/style.go:292` - Called in hot path during selector matching

## Problem

```go
func (n *Node) Classes() []string {
class := n.GetAttribute("class")
// Parses class string every time - O(n) string parsing
classes := []string{}
start := 0
for i := 0; i <= len(class); i++ {
// ...
}
return classes
}
```

This is called for every selector match attempt, meaning the same class string gets parsed hundreds or thousands of times for a single element.

## Suggested Improvements

1. **Cache parsed classes on the Node struct** - Store `parsedClasses []string` and invalidate on attribute change
2. **Use `strings.Fields()`** - More efficient than manual parsing
3. **Lazily initialize and cache** - Parse once on first access

## Impact

For a page with 100 elements and 50 CSS rules with class selectors, this causes ~5000 redundant string parsing operations.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in dom/node.go:85-101 to inspect Classes(), then trace attribute changes on Node and its use from style/style.go:292. Choose a caching approach that remains valid after class attribute updates, and confirm selector matching no longer reparses the same class string on every attempt.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.