Skip to content

Add benchmark for a style-heavy document - #642

Merged
mganss merged 1 commit into
mganss:masterfrom
jafin:bench/email-stylesheets
Aug 19, 2026
Merged

Add benchmark for a style-heavy document#642
mganss merged 1 commit into
mganss:masterfrom
jafin:bench/email-stylesheets

Conversation

@jafin

@jafin jafin commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Added a CSS-heavy benchmark

Cherry picked from #639 without any optimizations to code

The existing benchmarks barely exercise CSS:

  • ecmascript.html: no styled elements
  • google.html: only 8 styled elements

CSS sanitization is relatively expensive, so a generated email.html benchmark was added to represent HTML email, where inline styles are common.

Method Mean Allocated
SanitizeGoogle 6.896 ms 6.02 MB
SanitizeEmail 22.486 ms 15.71 MB
SanitizeEmailWithStyleSheets 24.147 ms 16.89 MB

Despite being much smaller than google.html, the email benchmark takes roughly 3× longer and allocates over 2× as much, showing how expensive the CSS path can be.

Two benchmarks are included:

  • SanitizeEmail — normal inline-style sanitization
  • SanitizeEmailWithStyleSheets — also enables <style> so stylesheet sanitization is measured

The suite left the CSS path almost entirely unmeasured. ecmascript.html has
no styled element at all and google.html has eight of 309, yet sanitizing a
style attribute costs far more per byte than sanitizing ordinary markup:
the new 63 KB document takes 22.4 ms and allocates 15.5 MB, against 7.0 ms
and 6.0 MB for a google.html two and a half times its size. Per byte that is
roughly six times the cost, and it was invisible.

HTML email is where that case really arises, since mail clients strip
stylesheets and templates inline everything instead. email.html is generated
rather than taken from a real campaign, so nothing third-party is vendored
in, but its shape is calibrated against a real responsive template: about
45% of elements carry a style attribute averaging 3.3 declarations, drawn
from the property mix that template actually uses. Values differ per element
so the CSS parser cannot cache its way to a number no real document would
produce, and background-image: url(...) is kept to a minority because the
reference template has none inline - over-representing it would flatter any
change aimed at URL handling.

Two benchmarks, because <style> is not an allowed tag by default and the tag
is therefore dropped before its content is looked at: SanitizeEmail measures
the inline path as a caller gets it out of the box, and
SanitizeEmailWithStyleSheets allows the tag so that SanitizeStyleSheets, and
the CSS parsing and regex work behind it, are on the measured path. The
difference between them is 3.3 ms and 1.2 MB for 8.1 KB of CSS.
@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.47%. Comparing base (20031db) to head (eb42276).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #642   +/-   ##
=======================================
  Coverage   93.47%   93.47%           
=======================================
  Files           6        6           
  Lines        1119     1119           
  Branches      152      152           
=======================================
  Hits         1046     1046           
  Misses         47       47           
  Partials       26       26           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mganss
mganss merged commit e1f020f into mganss:master Aug 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants