Skip to content

Speed up html_classes() - #5032

Open
Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/html-classes-single-pass
Open

Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/html-classes-single-pass

Conversation

@Kocal

@Kocal Kocal commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

html_classes() built a list, then ran array_filter() with a closure, array_unique() and implode().

It now collects the classes keyed by themselves, skipping empty strings, and implodes once. The first occurrence wins as before.

8 distinct strings go from 902-938 -> 465-485 ns, and 3 strings + a 3-entry map from 702-723 -> 370-406 ns.

`html_classes()` built a list, then ran `array_filter()` with a closure, `array_unique()` and `implode()`.

It now collects the classes keyed by themselves, skipping empty strings, and implodes once. The first occurrence wins as before.

8 distinct strings go from 902-938 -> 465-485 ns, and 3 strings + a 3-entry map from 702-723 -> 370-406 ns.
@Kocal
Kocal force-pushed the perf/html-classes-single-pass branch from 5b2c45d to dd8c421 Compare November 15, 2026 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants