Performance wall: how many elements a canvas can draw
Each row keeps adding elements, all moving every frame, until the frame rate drops. The same scene is run as plain DOM with CSS transforms,
as a 2D canvas drawing every element with drawElementImage, and as a worker drawing transferred ElementImage snapshots.
The numbers are measured on your device when you press Run.
Not run yet.
Show the code the three scenes being timed
- The canvas rows: every card is a child of one drawable canvas.
htmlis the markup ofncards. - 2D row: draw every element on each
paint. A frame loop changes some text and callscanvas.requestPaint(). - Worker row: the canvas goes to a worker, and only elements that changed get a new snapshot.
- The worker. It keeps the newest snapshot of each element and draws them all every frame, whatever the main thread is doing.
Cells are frames per second with that many elements (for worker rows: the worker's frame rate, which is what moves on screen). The wall is the largest count that held 90% of this display's refresh rate. A row stops at the first count under half the refresh rate.
Latency: from a DOM change to the frame that shows it
The wall above measures frame rate. This measures delay. Each sample adds a line of text and reads when the browser presented it, from the element's Element Timing entry. The fixtures are drawn in the box at the top of the page.
| Route | Median | Detail |
|---|---|---|
| Not run yet. | ||
Where a long frame goes
From long-animation-frame entries, which exist only for frames over 50 ms. The scene is a grid of small cells whose text all changes every frame.
The cell count doubles until plain DOM frames are that long, and the canvas then draws the same grid.
| Scene | Frame | Script | Style, layout, paint | After the main thread | Detail |
|---|---|---|---|---|---|
| Not run yet. | |||||
Show the code latency, long frames and the device tier
- The change being timed. A new element with an
elementtimingattribute. - 2D canvas: compare the time of the
paintevent with the presentation time. - A canvas that draws nothing still gets an entry.
- Worker: snapshot in the paint event, draw in the worker, report on the epoch clock.
- The latency worker.
- Long frames: split each entry at
renderStart,styleAndLayoutStartandpresentationTime. - The device tier:
navigator.cpuPerformance, or cores and memory. - Software WebGPU adapter check.
Device tier
Reading the result
- Run it to see what this device does.
Findings
- A 2D canvas adds no frame of delay. In Chromium 157 the
paintevent that draws a change runs in the same frame that paints the changed element, before that frame is presented. - Element Timing doesn't tell you the canvas drew anything. An entry with a
presentationTimearrives for a canvas child even when the paint handler never draws it. The time is when the element's picture was painted. - A worker canvas draws the snapshot within a few milliseconds. The snapshot is taken in the paint event of the same frame as the change and the worker draws it on its next frame: a median of 3 ms after the change in the test browser. When the worker's frame reaches the screen can't be read from the page.
- The canvas's extra cost in a long frame comes after the main thread. With a grid of 16,000 cells, the main-thread part of the frame was 115 ms as plain DOM and 108 ms in a canvas. The time from the end of the frame to its presentation was 4 ms as plain DOM and 61 ms in a canvas.
drawElementImage()itself took 3.8 ms. - The
painthandler isn't listed in a long frame'sscripts. Only therequestAnimationFramecallback is. - Open: the numbers above are from a headless desktop browser with software rendering. A phone's numbers come from the two buttons. Chromium 157 has no separate style and layout durations on the entry; the table shows them when the browser provides them.
What the rows mean
- Plain DOM is the baseline: the same cards, absolutely positioned, moved with
transform. The compositor does the work. - 2D canvas asks for a
paintevent every frame and redraws every element. The cost is onedrawElementImagereplay per element per frame, on the main thread. - 2D canvas, changing content also rewrites the text of some elements each frame, so their paint records are rebuilt.
- Worker transfers the canvas to a worker. The main thread only captures an
ElementImagewhen an element changes; the worker redraws every frame from the snapshots it holds. - Busy main thread adds 40ms of blocking script to every main-thread frame. Canvas drawing on the main thread drops to that rate; the worker does not.