JS-modulesnetflow-sankey
2.16.0 · API 2

netflow-sankey

Lays out a flow chart (Sankey) from a plain list of links, with about half the weighted crossings of d3-sankey on the charts measured, backward links drawn instead of crashing, and an SVG drawer.

What it is and how it works

netflow-sankey lays out a flow chart (a Sankey diagram) from a list of links (from, to, value) and can draw it as an SVG. One file, 28.4 KB gzipped (19.5 KB minified and gzipped), no dependencies. Version 1.x had a different name; a project on 1.x only changes the folder, file and global to netflow-sankey, netflow-sankey.js and NetflowSankey.

Why it exists

A flow chart is read through its thick bands, so a crossing between two big flows matters far more than one between thin ones. d3-sankey counts every crossing the same and throws an error when the data has a loop. On three test charts netflow-sankey left about half the weighted crossings of d3-sankey.

Where it helps

Not for: pages that already ship D3, have no loops and want the smallest code (d3-sankey is about 2 KB gzipped).

How it compares

netflow-sankey is bigger than d3-sankey, but on four real retail revenue charts (72 to 1,500 links) it leaves 12% to 77% of d3-sankey's weighted crossings (lower is better), and it draws a chart with loops where d3-sankey stops with an error. Speed against d3-sankey on real retail revenue flows (country to month to product, the top links by value; ungoogled Chromium 156, Firefox 158, WebKitGTK 2.52.6, 2026.11.22): at 72 links d3-sankey is faster in Chromium (0.39x) and Firefox (0.5x) and level in WebKitGTK; at 200 links it is level or ahead (Chromium 1.17x, Firefox 0.67x, WebKitGTK 1.0x); at 500 links this module is 1.0 to 1.5 times as fast; at 1,500 links 2.1 to 3.8 times as fast. The earlier 3.7 to 5.5 times (laptop Firefox 160, generated charts) did not hold on real data.

AlternativeSize (smaller is better)SpeedFeaturesWhere it wins / loses
d3-sankey (BSD-3-Clause, 0.12.3)5.7 KB minified, 2.0 KB gzipped, plus d3-array and d3-shape (their size: not measured). This module: 81,657 bytes readable, 28.4 KB gzipped; 47,326 bytes minified, 19.5 KB minified and gzipped (terser 5.51.2)Real retail flows, three test browsers: d3-sankey faster or level up to 200 links (down to 0.39x), this module 1.0 to 1.5 times as fast at 500 links and 2.1 to 3.8 times at 1,500No loops (throws "circular link"); crossing order not weighted by flowWins on size. Loses on weighted crossings (12% to 77% as many on real charts) and on loops
d3-sankey-circular and similar forks (version and license: not read)Not measuredNot measuredAdd loops to d3-sankeySame job as the loop support here; pictures not compared
Plotly and Google Charts Sankey (versions: not read)Not measured (each is a whole charting library)Not measuredSankey as one chart type among manyPick one if you already ship it; this module is only the Sankey layout and SVG

The browser race of Netflow Map against D3 measured the app's own flow engine, not this module, so it gives no browser number for netflow-sankey.

Where it loses

What it does differently

Numbers (Node 22, measured 2026-10-17, against d3-sankey 0.12.3)

> Node numbers. The speeds in this section were measured in Node, not in the three test browsers (ungoogled Chromium 155.0.8059.39-1, Firefox 158.0, WebKitGTK 2.52.6), so they are not what a visitor's browser does. Use them to compare rows with each other, not as page speeds. Sizes and counts are exact; only the times depend on the browser. Re-measuring in the three browsers is not done (open item on TASKS.md).

Four real charts (2026.11.22): retail revenue flows, country to month to product, the top 72, 200, 500 and 1,500 links by value. One yardstick for both: for every pair of bands between the same two columns that cross, add their two flows multiplied together. Lower is better. Counts are exact and do not depend on the browser; the harness is tools/real-corpus.

72
netflow-sankey as a share of d3-sankey
2.10e12 · 2.95e1271%
200
netflow-sankey as a share of d3-sankey
4.44e12 · 5.94e1275%
500
netflow-sankey as a share of d3-sankey
5.60e12 · 45.68e1212%
1,500
netflow-sankey as a share of d3-sankey
7.94e12 · 10.36e1277%

So 12% to 77% of the weighted crossings, at 0.4 to 3.8 times the speed of d3-sankey depending on size and browser (table in the Speed paragraph above). The two tools place the columns differently, so this is one yardstick on four charts, not a proof for every chart. The earlier generated charts (64, 118 and 222 links; 47% to 58%) were replaced. A chart with a loop (a to b to c to a): d3-sankey throws "circular link"; netflow-sankey draws it.

How to use it

Try it and test it

Test it: node modules/netflow-sankey/test/run.js (31 checks, about 1 s). Try it: demo.html (type links, the chart redraws).

Use

Load netflow-sankey.js (nothing else needed). One call, data only:

svgElement.outerHTML = NetflowSankey.toSvg([{ from: "Wages", to: "Rent", value: 40 }, { from: "Wages", to: "Food", value: 25 }]);
containerDiv.innerHTML = NetflowSankey.toSvg([{ from: "Wages", to: "Rent", value: 40 }, { from: "Wages", to: "Food", value: 25 }]);   // the same call into a <div> you already have

value, weight or count names the amount. Options (the same words in every chart module) go in a second argument: NetflowSankey.toSvg(links, { title: "Where the wages go", height: 360 }). The two steps below give you the layout to draw yourself.

Your table can keep its own column names: toSvg(rows, { from: 'Source', to: 'Target', value: 'Amount' }), or leave them out and the first two text columns and the first number column are used (rows straight from a CSV work). toSvg returns SVG text (a string): put it in the page with element.outerHTML = ... or save it as a .svg file. title is optional: it draws a bold title above the picture and names the picture for screen readers; leave it out for none.

var R = NetflowSankey.layoutFlows({ links: [
  { from: "Wages", to: "Rent",  value: 40 },
  { from: "Wages", to: "Food",  value: 25 },
  { from: "Food",  to: "Wages", value: 3 }     // a link that runs backwards is fine
] }, { height: 360 });
svgElement.innerHTML = NetflowSankey.toSvg(R, { title: "Where the wages go" });

layoutFlows(input, opts) returns { nodes, bands, loops, width, height, simplified, note, ky, crossingWeight, ignored, ms } (simplified is how many very long links were drawn as one straight band because routing all of them would be too slow, 0 when none, and note is a sentence saying so; the cap is 20,000 stand-in points):

Dragging a bar (since 2.14.0): layout() results carry a hidden state. Pass it back as opts.resume (same items, same settings and fixedOrder, only fixY different) and the order, sizes and relaxed places are not worked out again: 10,000 items and 15,000 links take 0.32 (Chromium 156), 0.22 (Firefox 158) and 0.28 (WebKitGTK 2.52.6) of the time per move, with the same picture. A state from other settings is ignored and the full layout runs.

For Netflow Map's engine (2.1.0): NetflowSankey.options(sk) cleans up a person's settings (nodeW, gap, pad, height, fixY, justify, order; anything missing or out of range gets its default or the nearest limit) into the opts that layout() reads, and NetflowSankey.apply(L, r) writes a layout(L, opts) result r into the engine's drawing L: bar positions (x, y, w, h, cx, cy, layer, plus skIn, skOut, skPinned), the picture's width and height, dir = 'LR', and L.sk = { bands, loops, ky, order, cols, crossingWeight, ms }. With netflow-engine: NetflowEngine.use('sankey', NetflowSankey).

opts: height (aim for this picture height), nodeW, gapLayer (space between columns), pad (space between bars), restarts and budgetMs (how hard to search for a good order), stall (since 2.16.0, on by default above 150 items: stops the restarts after 3 in a row that found no better order, about 2 times faster at 500 links with the same picture on the three real flows tried, but a later restart can find a better order on some networks; stall: 0 runs every restart), quick (true: skip the extra restarts for speed on small charts; see below), seed, unweighted, justifySinks, and stopCap with stopCapFrom (off by default: on maps with at least stopCapFrom items, default 1,000, at most stopCap stand-in stops; the lightest long links beyond that are drawn as one straight band each, which is faster but a different picture: at 1,500 items stopCap: 500 takes about 4 ms instead of 24 in Node). toSvg takes colors, labels, fontSize, textColor, bandOpacity, and the optional title, subtitle and source (a bold title and lighter subtitle drawn above the picture, a small source line below; the title is also the picture's hidden label for screen readers); highlight (optional, since 2.8.0) names one item by id or label: that item and every band that touches it keep their colors, and the rest fade toward the background, the same word as in netflow-plot. Since 2.11.0 toSvg also takes valueLabel ('none', 'under', 'inline', 'percent' or 'both'), unit (a money sign before the number, a word after; unitAt decides it instead), percentOf ('total' or 'column') and notes ([{ text, x, y, anchor, size, color, bold, align }]: plain text at 0 to 1 across and down the picture, a thin leader line to anchor (an item or "from>to"), live words {total}, {value:id}, {share:id}, {flow:from>to}, {top} (the biggest flow with its amount, "Salary to Rent, $1,800") and, since 2.12.0, {topbox} (the biggest item with its amount, "Salary, $3,000")). These are the same words netflow-scene reads. A note past the edge widens the picture. All text is escaped. fromLinks, layout, layering, crossWeight, roundPath and toBezier are exported for people who want the pieces.

Who uses it

Used right now in Netflow Map (its repository is private, so there is no link). Checked in each app's shipped file on its main branch on 2026.10.29. The module's own page is demo.html in this folder.

Good to know

Limits

| Links | Chromium | Firefox | WebKitGTK | |---|---|---|---| | 72 | 1.6 / 0.7 / 1.2 | 2 / 1 / 1 | 1 / 1 / 1 | | 200 | 2.7 / 1.5 / 1.6 | 3 / 2 / 2 | 2 / 1 / 2 | | 500 | 8.3 / 2.0 / 4.8 | 7 / 2 / 6 | 5 / 2 / 7 | | 1,500 | 46.9 / 4.6 / 15.5 | 47 / 6 / 17 | 31 / 5 / 15 |

Quick mode is level with or faster than d3-sankey at every size; the default is slower than d3-sankey at 500 links in WebKitGTK (7 against 5 ms) and level elsewhere below 500 links. Firefox and WebKitGTK report whole milliseconds, so the small cells carry a rounding of up to 1 ms. Crossings for the default are in the Numbers section; the earlier generated-chart figures (74 to 78% for quick, 68 to 73% for the default) were removed and quick mode's crossings have not been re-counted on real charts. Tidiness of quick mode on the same real flows (weighted crossings, share of d3-sankey's, 72 / 200 / 500 / 1,500 links; Node count, same in every browser): quick 0.75 / 0.75 / 0.12 / 0.77 against the default 0.71 / 0.75 / 0.12 / 0.77, so quick mode is at most 5% above the default and still well under d3-sankey's crossings at every size.

Speed and efficiency numbers

Measured on a real laptop: Firefox 160 (Windows), 16 cores, 2026.10.30, 7:09 PM

Medians; lower is better for every time, size and ratio. This is one browser on one machine, and Firefox 160 is not one of our three cloud test browsers, so treat it as direction, not a guarantee.

Compared: netflow-sankey against d3-sankey (ms).

graphnetflow-sankey (default)restarts 0d3-sankeyd3 / ours
72 nodes2.00.61.00.5x (d3 is faster)
200 nodes3.0124.0x
500 nodes12625.2x

On small graphs d3-sankey is faster with the default settings. The default searches for fewer crossings, about 6% fewer than with restarts 0. From 200 nodes up, netflow-sankey is 4x to 5x faster.

Against d3-sankey: quick: true wins small charts on speed

Opt-in. Cloud-browser runs (shared machine, not the laptop). A 100-item chart, warm, lower is better: Chromium 0.50 ms against d3-sankey's 0.72, Firefox 0.60 against 1.00, WebKitGTK 0.60 against 0.80. It still draws 74 to 78% of d3-sankey's weighted crossings. The default is unchanged (about 2 ms, 68 to 73% of d3-sankey's crossings).

Against d3-sankey in Firefox 160 (ms): 72 nodes 2.0 (restarts 0: 0.5) against 1.0, so d3 is faster with the default settings there; 200 nodes 3.0 against 11 (3.7x); 500 nodes 11 against 61 (5.5x).

New option stopCap, off by default

It draws more long links straight on big maps. Node, layout time in ms and crossings (counted along link center lines), lower is better:

ItemsDefaultstopCap: 500d3-sankey
1,50024.4 ms, 71,0933.9 ms, 60,42318.9 ms, 99,058
5,00093.6 ms, 1,133,25715.7 ms, 462,76089.6 ms, 935,756

In Node, d3-sankey is faster than the default at these sizes, and the default draws more crossings than d3-sankey at 5,000 items. With stopCap: 500 this module is faster than d3-sankey and draws fewer crossings at both sizes. Browser runs of stopCap: not yet.

Against d3-sankey: tidiness, four real retail charts (2026.11.22)

Compared: netflow-sankey against d3-sankey 0.12.3 on real retail revenue flows, country to month to product (weighted crossings in trillions, lower is tidier; for every pair of bands between the same two columns that cross, the two flows multiplied together).

netflow-sankey leaves 12% to 77% of d3-sankey's weighted crossings on four real retail charts
72 links
netflow-sankey2.10
d3-sankey2.95
200 links
netflow-sankey4.44
d3-sankey5.94
500 links
netflow-sankey5.60
d3-sankey45.68
1,500 links
netflow-sankey7.94
d3-sankey10.36

Counted in Node, so the numbers do not depend on the browser. The two tools place the columns differently, so this is one yardstick on four charts. d3-sankey throws "circular link" when the data has a loop; netflow-sankey draws it. The earlier three generated charts (47% to 58%) were replaced.

Source: Against d3-sankey: tidiness, four real retail charts (2026.11.22).

Against d3-sankey: speed on real retail flows, cloud-browser runs

Compared: netflow-sankey against d3-sankey on real retail revenue flows (laid out warm, ms per chart, median of 5, lower is faster; 1,500 links, Chromium 156 / Firefox 158 / WebKitGTK 2.52.6).

With quick mode netflow-sankey lays out 1,500 real links in 4.6 to 6 ms against d3-sankey's 31 to 47, and the default setting takes 15 to 17 ms
Chromium 156
netflow-sankey, quick: true4.6
d3-sankey46.9
netflow-sankey, default15.5
Firefox 158
netflow-sankey, quick: true6
d3-sankey47
netflow-sankey, default17
WebKitGTK 2.52.6
netflow-sankey, quick: true5
d3-sankey31
netflow-sankey, default15

Real retail revenue flows, country to month to product, the top links by value. Quick mode is level with or faster than d3-sankey from 72 to 1,500 links (full table in the README); the default is slower than d3-sankey at 72 to 200 links in Firefox and at 500 links in WebKitGTK. Firefox and WebKitGTK report whole milliseconds. The earlier generated 100-item figures were removed.

Source: Against d3-sankey: speed on real retail flows, cloud-browser runs.

More

WebAssembly (since 2.3.0)

What. The inner loop of the order search (counting weighted crossings, sorting each column by the average position of its neighbors, trying swaps; wasm/order.c, 5,319 bytes) also exists as WebAssembly, embedded in netflow-sankey.js and used first. The restarts, the random numbers and everything else stay JavaScript, so a layout comes out the same as before: 48 test layouts identical to the last digit, with and without the CSP word, in each of 12 browser runs. NetflowSankey.wasmAvailable() says whether the browser allows it; layout(..., { wasm: false }) forces the JavaScript code.

Why. Median of 3 passes of 5 rounds in rotating order, JavaScript to WebAssembly in ms (lower is better; times faster, higher is better; WebKit's clock moves in whole milliseconds, so quick layouts there are timed 10 in a row). Only the order search is faster; the rest of a layout is still JavaScript, so big charts gain little.

ChartChromium 155Firefox 158WebKitGTK 2.52.6
small, 70 bars3.0 to 1.9 (1.58x)3.2 to 1.7 (1.88x)2.1 to 1.7 (1.24x)
medium, 240 bars19.3 to 11.7 (1.65x)23.1 to 14.0 (1.65x)14.8 to 14.2 (1.04x)
dense, 320 bars7.6 to 6.5 (1.17x)7.3 to 7.0 (1.04x)6.0 to 6.1 (0.98x)
large, 640 bars5.9 to 5.1 (1.16x)6.4 to 6.1 (1.05x)4.8 to 4.9 (0.98x)
big, 1000 bars7.2 to 6.5 (1.11x)8.9 to 8.3 (1.07x)6.3 to 6.3 (1.00x)
first layout, right after the page loads40.4 to 30.7 (1.32x)34 to 22 (1.55x)31 to 29 (1.07x)
first layout, 250 ms after the page loads43.4 to 29.4 (1.48x)36 to 22 (1.64x)26 to 26 (1.00x)

Chromium and Firefox rows are the median of 3 passes of 5 rounds; WebKitGTK's chart rows were run again as 3 passes of 9 rounds (a first run had shown two cells 9 to 10% slower, which did not repeat); the first-layout rows are the median of 11 fresh pages.

Use.

Honest limits. Gains are in Chromium and Firefox (up to 1.9x on small charts, about 1.05 to 1.2x on big ones). In WebKitGTK the chart rows are a wash (0.98x to 1.24x). Costs bytes: +11,578 as written (37,790 to 49,368), +9,493 minified (19,377 to 28,870) and +4,301 gzipped (8,097 to 12,398) with terser 5.51.2 (smaller is better). Not run: a phone, Safari.

Credits

No code from others; the ideas above are theirs.

Original sources (links checked 2026-11-14; ideas only unless the section above says code is included):