Performance

A two-canvas Canvas2D pipeline repaints only what changed, and every per-frame step is bounded by what is on screen, not by how much history is loaded. The numbers below come from pnpm bench (single core) and from profiling the live widget; the Feature Lab times real switches as you click.

Frames: flat from 500 to 100,000 bars

  • Two canvases — a scene canvas (grid, series, indicators, chart objects, axes) and a thin top canvas for the crosshair, legend and other pointer-tied visuals. A hover repaints only the top canvas (~0.2 ms), and the browser composites two surfaces, not four.
  • Visible-range rendering — every renderer, the auto-scale and the indicator price range walk only the bars in view.
  • No per-frame garbage — viewport snapshots are cached between changes; number and date formatters are reused instead of rebuilt per label.
  • Auto-sized price axis — fitting the axis to its longest label costs ~0.05 ms and only relayouts when the width really changes.

Live ticks: incremental indicators

A tick only moves the forming bar, so an indicator with its own update() recomputes just that bar: ATR, Bollinger Bands, Chande Kroll Stop, DEMA, EMA, Envelope, KAMA, MACD, MA Cross, McGinley Dynamic, OBV, RSI, SMA, SMMA, Stochastic, Ulcer Index, Volume Oscillator, VWMA, WMA. Another 31 are built on PointwiseIndicator and resume from the last bar the same way, and an indicator that reads another indicator’s line resumes too. When the bars an indicator remembers no longer match, it recalculates in full. BB + EMA + RSI + MACD:

HistoryFull recalculationIncremental update()
20,000 bars~5 ms~0.0005 ms
100,000 bars~27 ms~0.001 ms

Custom indicators can opt in too — see Plugins → fast live updates.

Switching symbol or timeframe

  • Cheap full loads — indicator values live in an IndicatorValueMap (array-backed, ~3× cheaper to build than a timestamp-keyed Map), and setData reuses already-valid bars instead of copying them.
  • Loading only when it is slow — the previous chart stays up; a veil appears only if a switch outlasts 200 ms, so a normal ~130 ms network switch never flashes.
  • No stale data — superseded history requests are dropped and old sockets detached, so the last click always wins.
  • Local resampling — static data switches timeframe without refetching; slow resamples paint the veil before the main thread gets busy.

LTTB downsampling

Line and area charts downsample the visible range to ~2 points per pixel with Largest-Triangle-Three-Buckets when there are far more bars than pixels — the line stays visually identical while drawing dozens of times fewer points. A no-op at normal zoom. The algorithm is exported for your own use:

import { lttbDownsample } from '@tradecanvas/chart'

// indices preserving the shape of a 100k series, reduced to 1600 points
const idx = lttbDownsample(series.length, 1600, (i) => series[i].close)
Visible points → 1600Time / frameThroughput
10,000~0.025 ms39,600 / s
100,000~0.32 ms3,100 / s
1,000,000~2.6 ms380 / s

Indicators zoomed out

Below a pixel per bar, indicator lines, bands and histograms draw one span per pixel column: the column's lowest to highest point, joined to the column before, as wide as the line. It looks much the same as a stroke through thousands of points for a fraction of the raster work. Zoomed out on 200,000 bars with Bollinger Bands, EMA, RSI and MACD, a frame went from about 54 ms to about 21 ms on integrated graphics. node scripts/bench-render.mjs runs these numbers on your own machine.

WebGL renderer (preview)

renderer: 'webgl' draws the plot and indicator panes with WebGL 2, on a canvas under the 2D scene: the grid, sessions, candles and volume directly, and the Canvas 2D drawing of indicators, compare lines and most chart types recorded as GPU strokes, fills and rectangles, antialiased at their edges as Canvas 2D is. The depth heatmap and the volume and market profiles are recorded the same way, under the bars; a market profile showing its stats readout or letters stays with Canvas 2D. Text, drawings, orders, axes and the crosshair stay on Canvas 2D, as does anything the GPU wouldn't draw the same (left to Canvas 2D in order, so the stacking doesn't change); custom indicator plugins come along without changes. Candles land on the same device pixels as with Canvas 2D; lines and fills differ only on a few antialiased edge pixels. The WebGL code is a chunk of its own (about 17 KB gzipped), loaded on first use; where WebGL 2 is missing, or its context is lost, the chart carries on with Canvas 2D. If the context comes back, WebGL does too. At most 8 charts on a page draw with WebGL at once (setMaxWebGLCharts), as Chrome and Safari keep only about 16 WebGL contexts a page and drop the oldest past that; a chart past the limit draws with Canvas 2D until one is let go. Checked in Chrome, Firefox and WebKit.

const chart = new Chart(el, { renderer: 'webgl' })

chart.on('rendererChange', (e) => console.log(e.payload))
// { renderer: 'webgl', reason?: 'contextRestored' }, or { renderer: 'canvas', reason: 'unsupported' | 'contextLost' | 'limit' }

await chart.setRenderer('canvas')   // resolves to what draws now

setMaxWebGLCharts(4)   // from '@tradecanvas/chart': at most 4 charts on the page draw with WebGL (8 by default)

Frame time while panning, on integrated graphics (Intel UHD; 16.7 ms is 60 fps):

ChartPixel ratioCanvas 2DWebGL
1600×900, 500 candles + 4 indicators227.4 ms19.6 ms
1600×900, zoomed out on 200,000 bars + 4 indicators234.5 ms20.2 ms
1600×900, zoomed out on 1,000,000 bars + 4 indicators234.5 ms16.7 ms
1600×900, depth heatmap, 240 snapshots × 80 levels225.5 ms16.8 ms
Six charts, 500 candles and two indicators each223.5 ms17.2 ms
2560×1400, 2,000 candles + 4 indicators141.6 ms17.7 ms
2560×1400, 2,000 candles + 4 indicators1.570.8 ms17.6 ms
2560×1400, 2,000 candles + 4 indicators2114.5 ms29.1 ms
2560×1400, 2,000 candles233.1 ms20.9 ms

Most WebGL frames above land on 16.7 ms; the averages carry a few longer ones. At a pixel ratio of 2 on a 2560×1400 chart, the browser's compositing of the full-size layers alone takes about 23 ms on this GPU. node scripts/bench-render.mjs --renderer=webgl runs these numbers on your own machine.

A bar never gets narrower than a quarter of a CSS pixel, so “zoomed out on 1,000,000 bars” shows about four bars per pixel (about 6,400 bars on a 1600 px chart); the scene pans through the rest of the million.

Phones and apps

A phone has a 3x screen, a slower CPU and a battery. The chart draws at a pixel ratio of 2 at most (maxPixelRatio): 56% fewer pixels than 3x, for lines barely softer. It stops drawing while it is off screen or the page is hidden (pauseWhenHidden), and an app embedding it in a WebView pauses it with suspend(). A live feed sending ticks faster than frames recalculates indicators once a frame, before anything reads them. When the page comes back or the network does, a stream silent too long reconnects at once instead of waiting for its heartbeat; a destroyed chart gives its canvas memory back at once.

new Chart(host, {
  maxPixelRatio: 2,       // the default; Infinity draws at the screen's own
  pauseWhenHidden: true,  // the default: no frames off screen or on a hidden page
})
chart.suspend()   // your app went to the background (a WebView): stop drawing
chart.resume()    // back: one frame draws what changed

Measured on a phone screen (390 × 844 at 3x, the CPU slowed 4x, an integrated GPU), one chart with four indicators:

BeforeNow
Draw the scene, 300 bars34 ms25 ms
Live, 2 ticks a frame18.5 ms11–13 ms
Canvas memory17.1 MB7.6 MB

The widget on a touch screen: inputs at 16 px so the page doesn’t zoom when one takes focus, sheets above the on-screen keyboard, heights in dynamic viewport units. To measure on your machine: node scripts/bench-render.mjs --mobile.

Ten minutes on a busy feed

A live desk left running: the full widget on 1,000,000 one-second bars, drawn with WebGL, four indicators, a position with its stop and target, working orders that are filled, placed again and moved, the bid and ask, a depth heatmap, and a feed of 30,000 trades a second from a worker standing in for the socket. Every trade reaches the chart as a tick. Every 30 seconds the soak collects garbage and reads what is still held.

What was readAt 1 minuteAt 10 minutes
JS heap after a collection418.0 MB418.3 MB
DOM nodes1,2201,220
Event listeners364364
Trades taken in, a second30,02729,998
Frames a second56.756.4
p95 frame18.2 ms18.2 ms
Main-thread tasks over 50 ms00

The heap grew 0.3 MB in nine minutes: the 600 new bars and their indicator values. Taking the trades in costs about 1% of the main thread; most of a frame is drawing. In 1.68.0 the same desk drew about 29 frames a second with a 50 ms task every second: the time axis looked at every bar's time on each frame, the day lines went over the whole history for each new bar, and the heatmap sorted each snapshot's prices on every frame. Headless Chrome on an Intel UHD 770. To run it on your machine:

pnpm build
pnpm soak                          # 10 minutes, 30,000 trades/s, 1,000,000 bars, WebGL
pnpm soak --minutes=30 --rate=50000 --renderer=canvas

What a page downloads

new ChartWidget() brings in 457 kB of JavaScript gzipped (1.69 MB before compression), its English and Vietnamese strings and its stylesheet included. The dialogs and panels a page has no need of as it loads (the indicators dialog, the command palette, the symbol search, the chart settings, an indicator's and a drawing's settings, Go to, the shortcut sheet, the dock's alerts, objects, symbol info, tape and depth ladder, the order ticket and the account panel) are 59 kB more. They load the first time one opens, or once the page is idle after the widget is up, whichever comes first; ChartWidget.preload() loads them at once, for a page that opens one from code as it starts. The indicator worker (indicatorWorker: 'auto') is 36 kB more, loaded the first time a chart sends it work, never with 'off'. A bundler puts them in files of their own; served straight from the package, they sit in its dist folder beside the entries, so copy the whole folder. @tradecanvas/core keeps a file for each of its modules there, so that a bundler can leave out what a page never uses: without one, a page asks for all of them. pnpm size, after pnpm build, prints these numbers for each entry.

The main thread

Indicators are computed on the main thread by default. With indicatorWorker: 'auto', computing them in full (on setData, a new symbol or interval, a new indicator or new parameters) over 50,000 bars or more goes to a Web Worker the package ships. Its code is inlined in a chunk that loads the first time it is needed and starts from a Blob URL, so there is nothing to serve, and one worker serves every chart on the page. The bars go to it as columns of numbers and the results come back the same way, made into points again a slice of bars at a time, so the page keeps drawing and answering input. Live ticks stay incremental on the main thread.

new Chart(el, { indicatorWorker: 'auto' })
new ChartWidget(el, { chartOptions: { indicatorWorker: 'auto' } })

Until a result arrives the chart keeps drawing the previous output: after a new symbol or interval, the previous series' lines; for an indicator just added, nothing. Reading values, with getIndicatorOutput or an export, first computes on the main thread whatever is still in the worker, so a read is never out of date, and an alert on a line still in the worker is checked when it lands; getIndicatorOutput(id, { drawn: true }) returns what is drawn without waiting. Indicators that read another indicator's line or another symbol, and your own (registerIndicator), are computed on the main thread, as is everything where workers can't run: a server, jsdom, or a Content Security Policy that refuses blob: workers.

setData, then EMA, RSI, MACD and Bollinger Bands200,000 bars, offauto1,000,000 bars, offauto
Main thread held by the calls74 ms21 ms269 ms43 ms
Tasks over 50 ms1010
Longest time between frames50 ms33 ms250 ms33 ms
Every line drawn after74 ms243 ms269 ms1,025 ms

The worker saves no work: making its results into points again costs the main thread about what computing them does. It splits that work into slices a frame can afford, at the price of the lines coming later, which is why it starts at 50,000 bars. Headless Chrome on a desktop, the median of three runs; pnpm bench times both halves in Node (recompute.bench.ts).