Commit c541a35
Optimize simplecov-html for large coverage reports
## Problem
A real-world coverage report with 1705 source files produces a **41MB
HTML file** (~1.9M lines) that causes significant browser slowdown:
- 96% of the file is the source files section (~41.3MB)
- 51% of that section is whitespace from ERB template formatting (~21MB)
- 142,254 `<li>` elements rendered into the DOM at page load (hidden)
- 142K event handlers individually bound to line number elements
- An unnecessary `<div>` wrapper around each `<li>` adds 142K extra DOM nodes
## Changes
### Phase 1: Template Optimizations
**ERB whitespace trimming** (`lib/simplecov-html.rb`, all `.erb` views)
- Add `trim_mode: '-'` to the ERB constructor
- Add `<%-` / `-%>` trim markers to control flow tags in all templates
- Eliminates ~21MB of blank lines produced by ERB conditionals/loops
**Remove `<div>` wrapper around `<li>`** (`views/source_file.erb`)
- These were semantically invalid inside `<ol>` and unnecessary
- Saves ~2.8MB and 142K DOM nodes
**Conditional `data-hits` attribute** (`views/source_file.erb`)
- Only emit `data-hits="N"` when `line.coverage` is truthy
- Previously emitted `data-hits=""` for ~83K "never" lines with no
JS/CSS referencing the empty attribute
**Compact `covered_percent.erb`**
- Single-line template avoids multi-line whitespace across ~3,400 calls
### Phase 2: Browser Performance
**`<template>` tags for source files** (`views/layout.erb`)
- Each source file is wrapped in `<template id="tmpl-SHA1">`
- `<template>` content is parsed but NOT rendered into the DOM until
explicitly activated — removes ~500K DOM nodes from initial page load
**Template materialization on demand** (`assets/javascripts/application.js`)
- New `materializeSourceFile()` function clones template content into
the `.source_files` container when a file is first viewed
- Syntax highlighting applied on materialization (same deferred approach)
- All code paths updated: click, colorbox onLoad, popstate, deep links
**Event delegation for line number clicks** (`assets/javascripts/application.js`)
- Replaced direct binding on 142K elements with a single delegated
event handler on `document`
- Required for `<template>` approach and eliminates 142K event bindings
## Results
Tested against a real coverage report with 1705 source files:
| Metric | Before | After | Change |
|---------------------|-------------|-----------|--------------|
| File size | 41 MB | 25 MB | **-39%** |
| Lines | 1,944,847 | 573,916 | **-70%** |
| Blank lines | 1,090,596 | 3,578 | **-99.7%** |
| `<div>` wrappers | 142,255 | 2 | **-142,253** |
| `data-hits` attrs | 142,253 | 59,101 | **-83,152** |
| DOM nodes at load | ~500K+ | ~few K | deferred via `<template>` |
| Event bindings | 142K | 1 | delegated |
The remaining 25MB is predominantly the actual source code content
inside `<li>` elements, which is irreducible.
## Files Modified
| File | Changes |
|---|---|
| `lib/simplecov-html.rb` | `trim_mode: '-'` on ERB constructor |
| `views/source_file.erb` | Trim markers, remove `<div>`, conditional `data-hits` |
| `views/layout.erb` | Trim markers, `<template>` tags around source files |
| `views/file_list.erb` | Trim markers on control flow tags |
| `views/covered_percent.erb` | Single-line template |
| `assets/javascripts/application.js` | Template materialization, event delegation |
| `public/application.js` | Re-compiled asset |
## Test Plan
- [x] `bundle exec rake test` passes
- [x] Generate a coverage report against a test project and compare HTML file size
- [x] Open in browser: file list loads, clicking a file shows source in modal
- [x] Line numbers are clickable and scroll to correct position
- [x] Syntax highlighting works on opened files
- [x] Back/forward navigation works correctly
- [x] Deep-linking to a specific file/line via URL hash works
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>1 parent 996daed commit c541a35
File tree
7 files changed
+108
-96
lines changed- assets/javascripts
- lib
- public
- views
7 files changed
+108
-96
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
13 | 31 | | |
14 | 32 | | |
15 | 33 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
| 34 | + | |
| 35 | + | |
24 | 36 | | |
25 | 37 | | |
26 | 38 | | |
| |||
36 | 48 | | |
37 | 49 | | |
38 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
39 | 55 | | |
40 | 56 | | |
41 | 57 | | |
| |||
56 | 72 | | |
57 | 73 | | |
58 | 74 | | |
59 | | - | |
60 | | - | |
| 75 | + | |
| 76 | + | |
61 | 77 | | |
62 | 78 | | |
63 | 79 | | |
| |||
75 | 91 | | |
76 | 92 | | |
77 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
78 | 98 | | |
79 | 99 | | |
80 | 100 | | |
| |||
123 | 143 | | |
124 | 144 | | |
125 | 145 | | |
| 146 | + | |
| 147 | + | |
126 | 148 | | |
127 | 149 | | |
128 | 150 | | |
129 | 151 | | |
130 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
131 | 157 | | |
132 | 158 | | |
133 | 159 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
38 | 36 | | |
39 | 37 | | |
40 | 38 | | |
| |||
46 | 44 | | |
47 | 45 | | |
48 | 46 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
55 | 53 | | |
56 | 54 | | |
57 | 55 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
75 | 73 | | |
76 | 74 | | |
77 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
| |||
31 | 30 | | |
32 | 31 | | |
33 | 32 | | |
34 | | - | |
35 | | - | |
36 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
20 | 18 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
30 | 26 | | |
31 | | - | |
32 | 27 | | |
33 | 28 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
55 | 44 | | |
56 | 45 | | |
57 | 46 | | |
0 commit comments