What it is and how it works
The engine of Netflow Map as a module you can copy and adapt. You give it items and links; it works out where everything goes and hands back plain numbers ready to draw.
- Data is prepared into flat typed arrays, with choke points and traffic found first.
- Items are placed with a layered, force or distance-based method, or as a story in rows, arcs, a matrix or a shape.
- Heavy steps run in WebAssembly when allowed, with identical results in JavaScript. It needs no page, so it runs in a worker or Node.
Why it exists
Netflow Map's gains live in its engine: a 30,000-item network went from 42 seconds to under a second in one week. Every significant engine update is published here so others can adapt it for their own cases.
Where it helps
- Network and flow maps in your own app.
- Laying out large graphs off the main thread.
- A starting point for custom layouts with add-ons.
Not for: drawing; it returns positions, not pictures.
How it compares
Run on 2026-10-18 against d3-force 7.9.0, Cytoscape cose 3.34.4, dagre 0.8.5 and ELK (elkjs) 0.12.0, each with its default options, in ungoogled-chromium 155.0.8059.39-1, Firefox 158.0, WebKitGTK 2.52.6, one browser at a time. Every case is a fresh page under script-src 'self' 'wasm-unsafe-eval'; 3 runs per case (a case over 20 s ran once); 60 s limit per case. The networks are generated from a fixed seed. Speeds are from the browsers only.
In short: the engine was faster in every case that finished, in all three browsers (higher is better for the engine): 3.2x to 4.8x faster than d3-force, 20.8x to 93.6x than Cytoscape, 304x to 614x than ELK's stress layout (300 items; nothing larger finished), 9.8x to 243x than dagre and 17.9x to 138.7x than ELK's layered layout. Where it gives something up:
- Cytoscape's force layout has slightly lower stress at 300 and 1,000 items (by 0.001 to 0.011, lower is better), and takes 21x to 94x longer; at 3,000 items it went over 60 s in all three browsers.
- ELK's layered layout has fewer crossings at 1,000 items (6,471 against 6,499, about 0.4% fewer) and at 3,000 items (35,909 against 37,247, about 3.6% fewer). At 300 items the engine has the fewest (559 against ELK 632 and dagre 784).
- ELK's stress layout needed 15.9 s to 26.4 s for 300 items (one run each) and went over 60 s at 1,000 and 3,000; its stress at 300 was 0.170 against the engine's 0.161.
Force (springs), network
First layout in a fresh page, lower is better, median of 3. Quality (stress, lower is better) is ungoogled-chromium's value.
| Items | Library | ungoogled-chromium | Firefox | WebKitGTK | Quality |
|---|---|---|---|---|---|
| 300 | Netflow engine | 113 ms | 93 ms | 104 ms | 0.178 |
| 300 | d3-force | 377 ms | 450 ms | 370 ms | 0.194 |
| 300 | Cytoscape (cose) | 2,361 ms | 3,943 ms | 2,880 ms | 0.167 |
| 1,000 | Netflow engine | 393 ms | 387 ms | 379 ms | 0.171 |
| 1,000 | d3-force | 1,505 ms | 1,768 ms | 1,429 ms | 0.194 |
| 1,000 | Cytoscape (cose) | 28.6 s (1 run) | over 60 s | over 60 s | 0.167 |
| 3,000 | Netflow engine | 1,399 ms | 1,462 ms | 1,584 ms | 0.190 |
| 3,000 | d3-force | 4,992 ms | 6,023 ms | 5,101 ms | 0.198 |
| 3,000 | Cytoscape (cose) | over 60 s | over 60 s | over 60 s | - |
Distances (stress), network
First layout in a fresh page, lower is better, median of 3. Quality (stress, lower is better) is ungoogled-chromium's value.
| Items | Library | ungoogled-chromium | Firefox | WebKitGTK | Quality |
|---|---|---|---|---|---|
| 300 | Netflow engine | 55 ms | 43 ms | 34 ms | 0.161 |
| 300 | ELK | 22.8 s (1 run) | 26.4 s (1 run) | 15.9 s (1 run) | 0.170 |
| 1,000 | Netflow engine | 233 ms | 209 ms | 228 ms | 0.159 |
| 1,000 | ELK | over 60 s | over 60 s | over 60 s | - |
| 3,000 | Netflow engine | 1,367 ms | 1,323 ms | 1,123 ms | 0.172 |
| 3,000 | ELK | over 60 s | over 60 s | over 60 s | - |
Layered, top to bottom
First layout in a fresh page, lower is better, median of 3. Quality (crossing link pairs, lower is better) is ungoogled-chromium's value.
| Items | Library | ungoogled-chromium | Firefox | WebKitGTK | Quality |
|---|---|---|---|---|---|
| 300 | Netflow engine | 38 ms | 28 ms | 17 ms | 559 |
| 300 | dagre | 369 ms | 346 ms | 455 ms | 784 |
| 300 | ELK | 714 ms | 500 ms | 862 ms | 632 |
| 1,000 | Netflow engine | 68 ms | 44 ms | 32 ms | 6,499 |
| 1,000 | dagre | 5,927 ms | 4,445 ms | 7,786 ms | 6,885 |
| 1,000 | ELK | 1,827 ms | 2,417 ms | 2,142 ms | 6,471 |
| 3,000 | Netflow engine | 116 ms | 91 ms | 80 ms | 37,247 |
| 3,000 | dagre | over 60 s | over 60 s | over 60 s | - |
| 3,000 | ELK | 8,404 ms | 12.6 s (1 run) | 7,688 ms | 35,909 |
"Over 60 s" means the case was stopped at the limit; a later browser did not run it again. The engine's quality differs by up to 0.007 between browsers on the same case, so the tables show Chromium's.
The libraries do more than this engine (drawing, routing, bundling, hundreds of options), so size is not like for like. Not measured: other graph libraries, tuned options, or real-world data. To rerun: modules/netflow-engine/bench/compare/README.md.
Force since 2.7.0 (real maps, 2026.11.26)
The real-map race below found Force behind Cytoscape on the SBA map and behind d3-force on the dense SEC map. 2.7.0 changes Force so it wins the stress score there while keeping fans of one-link items close to their center item (the look was chosen by the owner from side-by-side pictures):
- On sparse maps (under 4 links per item on average) a link between two hubs pulls harder, so hubs sit closer, and each fan is pulled in to 0.4 of the typical hub-to-hub link.
- Dense maps start from the Distances picture and take 40 Force rounds instead of 220, then each fan is held to at most one hub-to-hub link.
- Crowded pictures are stretched before the push-apart, as Distances already did.
Stress, lower is better (same score and maps as below; ungoogled Chromium 156.0.8078.79, Firefox 158.0, WebKitGTK 2.52.6, 3 runs each):
| Map | Items | 2.6.5 | 2.7.0 | Best rival |
|---|---|---|---|---|
| SBA | 300 | 0.162 | 0.146 to 0.148 | Cytoscape 0.144 to 0.148 |
| SBA | 922 | 0.184 | 0.165 to 0.167 | Cytoscape 0.170 |
| SEC | 300 | 0.230 to 0.252 | 0.187 | d3-force 0.217 |
| SEC | 1,000 | 0.184 to 0.191 | 0.174 | d3-force 0.186 |
| SEC | 2,683 | 0.167 to 0.169 | 0.175 | d3-force 0.187 |
| Retail | 3,000 | 0.215 to 0.221 | 0.201 to 0.203 | d3-force 0.219 |
| FHIR | 3,000 | 0.178 to 0.179 | 0.177 | d3-force 0.180 |
| Retail, FHIR | 300, 1,000 | 0.175 to 0.178 | same | d3-force 0.176 to 0.178 |
- Speed. Dense SEC maps take 1.7x to 2.9x less time, and 3,000-item retail 1.9x to 2.3x less. Every other case is level within page-to-page noise, checked again with 5 runs in each direction where a first pass read slower.
- Where it is not ahead. On the 300-item SBA map Cytoscape's score moves from run to run (it starts from random places). 2.7.0 beats it in Chromium and is 0.5% behind in WebKitGTK and 2.4% behind in Firefox. That is a tie at today's fan tightness: the best-scoring picture with its fans pulled in that close scored 0.153. On the 2,683-item SEC map 2.7.0 scores 4 to 5% higher than 2.6.5, still ahead of d3-force.
- Look. Pictures move. Fans stay as close to their center as before, but spread as wedges instead of round clusters, and on dense maps a big hub's fan sits apart from the core. The knobs are in
force2dTuning; the old Force is{ spread: 0, linkPow: 0, denseHi: 0, fanTight: 0, fanTightDense: 0 }.
On real maps (2026.11.22)
The comparison above uses generated networks. The same three-way race (bench/compare, REAL=<name>, maps cut by bench/compare/prep-real.js) was run on real maps: SBA awards (awarding agency to state to county to ZIP code, 922 items; the 300-item piece is its busiest part), retail customers to products, SEC filings to tags, and the FHIR training set's resource references (generated patients, owner-approved training data). Each map is the connected piece of 300, 1,000 or 3,000 items found by a walk from its best-connected item, with every link between them. First layout in a fresh page, ungoogled Chromium 156.0.8078.79, Firefox 158.0 and WebKitGTK 2.52.6, 2 runs per case, 30 s limit.
- Speed holds on every real map. The engine is 2.2x to 5.2x faster than d3-force, 22x to 57x faster than Cytoscape's cose at 300 items (nothing at 1,000 items or more finished in 30 s except one 922-item case), 327x to 507x faster than ELK's stress layout at 300 items (nothing larger finished), 4.4x to 28x faster than dagre and 3.2x to 45x faster than ELK's layered layout. On the 300-item SEC map (4,834 links) dagre and ELK's layered layout did not finish in 30 s; the engine took 0.9 to 1.1 s.
- Where the picture was worse in 2.6.5 (lower stress is better; 2.7.0 fixed this, see above). Cytoscape's cose beat the engine on the SBA map: 0.146 against 0.162 at 300 items and 0.170 against 0.184 at 922 items (the engine is 7 to 13% higher), at 22x to 57x the time. On the dense 300-item SEC map the engine is behind d3-force (0.237 against 0.217, 9% higher) and Cytoscape (0.228). It is ahead of d3-force on the SBA map (0.162 against 0.174) and on the larger SEC pieces (0.168 against 0.187 at 2,683 items), and level on the retail and FHIR maps.
- Not scored. Crossing counts for the layered layout are not computed on real maps (no row labels), so only the layered times are real-data figures.
- Limits. The SBA slice is the first 300 MB of the file and holds only 922 items. 3,000-item retail has 111,140 links. WebKitGTK ran the same cases except retail layered at 3,000 items, which neither dagre nor the engine finished there inside the limit. Cases over 30 s in one browser were skipped in the others. Raw results:
js-modules-research/audit-synthetic-claims-2026.11.22/engine-real-maps/.
How to use it
<script src="netflow-layers.js"></script> <!-- only for the story views (Flow, Top-down, Spine, Lanes, Sankey, Arc, Matrix); the Network view does not need it -->
<script src="netflow-engine.js"></script>
// one call, data only: links in, SVG text out (needs netflow-scene.js; the Sankey view also needs netflow-layers.js and uses netflow-sankey.js when it is there)
var svg = NetflowEngine.toSvg([{ from: 'Salary', to: 'Rent', value: 1800 }, { from: 'Salary', to: 'Food', value: 1200 }], { view: 'sankey' });
// the full form: a graph in, a layout out
var graph = { start: 'a', nodes: [{ id: 'a', label: 'Start', group: '', valence: '', ghost: false, attrs: {} },
{ id: 'b', label: 'Next', group: '', valence: '', ghost: false, attrs: {} }],
edges: [{ from: 'a', to: 'b', label: '', kind: '', attrs: {} }] };
var L = NetflowEngine.build(graph, { view: 'network', arrange: 'stress', colorBy: 'group' });
// L.nodes: one record per item with its place (cx, cy for the Network view; x, y for rows), size and color value
// L.edges: one record per link; L.groups: the groups; L.ms: how long the build took
A rough first picture while the map is still arriving
NetflowEngine.quickNetwork({ nodes: [{ id }], edges: [{ from, to }] }) places the items of a map that is only partly read on a spiral in breadth-first order from the best-connected item of each piece, with the pieces side by side and no overlap clean-up. It needs no build before it, writes nothing into your lists, and returns typed lists in item order: { n, cx, cy, size, width, height, rough: true } (centers and sizes in the units of the Network view). It takes 5 to 6 ms for 10,000 items and 24 to 60 ms for 50,000 (Chromium 156, Firefox 158, WebKitGTK 2.52.6), so a page can draw it between chunks of an import (with guess-gate at 5%) and replace it with the real layout when the map is complete. It shows size and spread, not structure: nothing is untangled.
Putting a big layout back (saved layouts)
A Network layout of a big map is slow to compute and quick to put back. NetflowEngine.layoutKey(graph, options) returns a short fingerprint string of everything that decides the picture (the data, the options, the tuning and the layout code itself; null for views and options where saving makes no sense, such as previews). Keep the finished positions anywhere you like, for example in IndexedDB, under that key:
var key = NetflowEngine.layoutKey(graph, opts);
var L = NetflowEngine.build(graph, Object.assign({}, opts, { saveKey: key, wantSave: true }));
if (L.saveLayout) store(L.saveLayout); // { key, xy: Float64Array of x,y per item, width, height, rough }
// later, for the same map:
var L2 = NetflowEngine.build(graph, Object.assign({}, opts, { saveKey: key, saved: fetched }));
// L2.fromSaved === true when the record matched; the picture is the same positions as a new layout. A wrong or damaged record is ignored and the layout runs as usual.
Netflow Map does this for maps of 1,500 items or more, reopening them 2.4 to 14.9 times faster (higher is better) with the same picture.
In Node: const E = require('./netflow-engine.js'); E.use('layers', require('../netflow-layers/netflow-layers.js')); (the layers module is also found on its own when it sits next to this folder).
Who uses it
Used right now in Netflow Map (its repository is private, so there is no link). Checked in its 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
- Story views need netflow-layers. The Sankey look needs an add-on with
options,layoutandapply(see above; netflow-sankey 2.1.0 or newer has all three). - It does not draw anything. Drawing (WebGL 2, plain canvas) lives in Netflow Map itself and is not part of this module.
- Item and link records follow Netflow Map's data shape (
nodeswithid,label,group,valence,ghost,attrs;edgeswithfrom,to,label,kind,attrs, optionalweight). Importers for CSV, JSON and other formats are in Netflow Map, not here. - The first layout in a fresh worker is slower than later ones (WebAssembly warm-up). Netflow Map warms its worker when idle; a page using this module can do the same by building a small map once.
Speed and efficiency numbers
Speed
Layout time, lower is better, one run each, cold start, the same network (1,000, 3,000, 10,000 items, Distances arrangement), in each browser one at a time:
The 10,000-item map is quicker than the 3,000-item one because the first big layout in a fresh page is cold (the wasm code warms up); a layout after that is about 1.4× faster in Chromium. Against the first version of Netflow Map's engine (2026-10-14), the same maps in the app took: 10,000 items 7.25 s to 456 ms (16×, Firefox 159), 30,000 items 42.1 s to 812 ms (52×), 100,000 items from over 60 s to 4.29 s. Netflow Map's numbers come from the app's build of this same engine (identical layouts, see Tests).
Since 2.6.0 the Distances layout of a big Network map takes 12 to 73% less time than 2.5.0 (48 cases: 16 maps of 1,500 to 50,000 items in Chromium 156, Firefox 158 and WebKitGTK 2.52.6), for example a 50,000-item drop in 1,105 to 1,460 ms instead of 2,904 to 3,502 ms, with the same or lower stress and fewer crossings. 2.6.1 takes about a fifth less again with the same picture (middle of 42 cases; up to 47% less). The tables on this page were measured before 2.6.0. 2.6.1 and 2.6.2 cut the build time again with exactly the same picture: 2.6.1 by about a fifth (hop counts from 64 items at once), 2.6.2 by about a tenth (leaner work around the layout; 39 of 42 cases faster, up to 31% less). 2.6.3 cuts maps shaped like a mesh or a tree by about a fifth more (9 to 26% less), again with the same picture. 2.6.4 fills the item and link records with less memory churn (about 7% less build time in Chromium, same output). Since 2.6.5 the every-pair fitting runs in single precision, four pairs at a time: 15% less build time in the middle case and up to 49% less under 4,000 items, with items in different spots than 2.6.4 and stress and crossings within a few percent.
Size
252 KB as one file, 82 KB gzipped (about 33 KB of it is the two WebAssembly helpers as text). No dependencies other than the optional add-ons above.
More
Views and arrangements
NetflowEngine.VIEWS lists the nine view ids in this order: flow, down, spine, lanes, shape, sankey, arc, matrix, network. In 1.0.0 the sixth id had another name; 2.0.0 knows only sankey (an old id draws the Flow picture, like any unknown id). The Shape view returns segs and spine (stretches between choke points) and no nodes. For network, arrange picks the method: stress (Distances, the default for big maps), closeness, force, radial, circle, rings, groups, grid, stress3d (Distances 3D), force3d, sphere, stack (NetflowEngine.ARRANGE_NAMES has the names; arrangeAdvice() says which fits a given map). Other options: colorBy, sizeBy, widthBy, edgeColorBy, laneBy, agg, mode, amounts (see the comment above build in the file).
spread: "wide" (Flow, Top-down, Spine and Lanes only; needs netflow-layers 1.3.0 or newer, an older copy ignores it) draws the story views looser and more like a tree for wide screens: more room beside each box, and a box over the middle of the boxes it leads to. "tight", or leaving it out, is the compact picture as before. Handy as a switch next to the view list.
Add-ons
- Layered drawing (netflow-layers 1.2.0 or newer; 1.3.0 for
spread: "wide"): required for every story view. Found as a globalNetflowLayers, or as../netflow-layers/netflow-layers.jsin Node, or handed over withNetflowEngine.use('layers', impl). If it is missing, the view that needs it throws a message that says so; the Network view still works. - Extra views (netflow-circular and netflow-hierarchy 1.0.0, since engine 2.3.0): optional. Chord and Bundles come from netflow-circular; Treemap, Sunburst, Icicle, Circle pack and Tree from netflow-hierarchy. Found as globals
NetflowCircularandNetflowHierarchy, or as the Node files next to this folder, or handed over withNetflowEngine.use('circular', impl)anduse('hierarchy', impl); their views then joinVIEWS.build(graph, { view: 'chord' })lays the map out (optionschordBy,chordOrder,nest,treeShape;sizeBysizes the boxes), and the add-on'sscene(L, options, NetflowScene)draws it. - Sankey add-on: optional (netflow-sankey 2.1.0 or newer).
NetflowEngine.use('sankey', impl)and passskoptions to the Sankey view; without it the Sankey view keeps the plain layered picture. The Sankey view calls three things on the add-on:options(sk),layout(layered, options)andapply(layered, result). netflow-sankey 2.0.0 haslayoutbut notoptionsorapply(Netflow Map's own copy has all three), so with 2.0.0 handed over, a Sankey view that is givenskoptions throws "SK.options is not a function"; a Sankey view withoutskoptions works. netflow-sankey 2.1.0 added the two calls, so use 2.1.0 or newer. - Your own: the engine's other parts (
separate,forceSim,networkLayout,communities,metrics,taskScore) are exported so an add-on can call them. An add-on never reaches into the engine's private storage. This is the same shape the Netflow Map add-ons use. NetflowEngine.columnsis the column reader (numbers as people write them, dates, categories, small formulas);status()returns{ placeholder: false, version, api }.
WebAssembly
The Force and Distances steps have a WebAssembly twin (forceWasm, stressWasm: each has on, minItems (150), calls, error). stressWasm also runs Distances 3D from min3d (3,000) items, the Closeness rounds from minClose (20,000) items and the hash-grid push-apart (counters calls3, closeCalls, sparseCalls); below those sizes the timings did not clearly favor WebAssembly. Flat Force starts from the Distances picture for pieces of 5,000 up to 20,000 items, and since 2.7.0 for dense pieces of any size under 20,000 (force2dTuning). The flat Distances sweeps also have a two-lanes-at-a-time form (stressWasm.simd, on by default where the browser supports it; any size since 2.2.1): two neighbors in the shuffled order that share no item run side by side, so the positions are the same to the last digit; turn it off with NetflowEngine.stressWasm.simd = false. Since 2.6.0 the Distances layout of a big Network map also runs these steps in the same program: the search for which pairs of items to fit, an every-pair fit for pieces of up to 4,000 items that keeps hop counts in one byte table and no pair list, and the push-apart that first stretches the picture once and then only revisits items that still touch. Since 2.6.1 the hop counts for that every-pair fit are found from 64 items at once. Turn one off with NetflowEngine.forceWasm.on = false; the plain-JavaScript path gives the same positions to the last digit (a check proves it). The only page-policy addition needed is 'wasm-unsafe-eval' in script-src; nothing else is loosened. Sources are in wasm/ (force.c, stress.c); sh wasm/build.sh rebuilds both (clang 18 with the wasm32 target and wasm-ld; two builds give the same bytes) and python3 wasm/embed.py puts them into netflow-engine.js.
Tests
node modules/netflow-engine/test/run.js (20 checks, about 10 seconds): every view on a 300-page story and a 1,500-item network, repeatable output, identical positions with and without WebAssembly (Force and Distances, 600 items; Distances 3D, Closeness and the hash-grid push-apart; since 2.6.0 also a 6,000-item Distances map and the new push-apart on its own), flat Force from 5,000 items, no two items on top of each other (2,000 items, three arrangements), odd inputs (a start that does not exist, an empty map, one item), use() names, the column reader, and that the file builds no code from text. Separately, Netflow Map checks that this file gives the same layout as its own engine: 315 builds (5 maps × 9 views × 7 option sets), 0 differences.
How updates ship
- The Netflow Map team finishes an engine change and writes a short note (what changed, in numbers, from the three test browsers).
- A new version of this module is published with a plain-words changelog entry. The module's file is cut from the app's engine by
tools/build-engine-module.mjsin Netflow Map, so the two cannot drift apart. - Netflow Map then uses the published version. Add-ons keep their own version numbers and say which engine versions they were tested with.
Version meaning: patch for a fix, minor for something new that breaks nobody, major when someone could be caught out (the API number equals the major).
Credits
- The engine is Netflow Map's own, by LFC Foundry. It starts from the layered, arc, matrix and shape drawings, choke points, traffic and drawing scores of the Story Editor's map (story-editor
editor/maplayout.js, MIT, copied and generalized; no dependency on it). - Choke points use the dominator tree of Cooper, Harvey and Kennedy (2001, linked below). The Distances (stress) layout minimizes stress by stochastic gradient descent (Zheng, Pawar and Goodman, 2018) and uses far-apart pivot items for big maps. The Force arrangement uses Barnes-Hut repulsion (a quadtree in 2D, an octree in 3D). Quick groups use label propagation. The source comments name no other published method.
- Since 2.6.0 the Distances layout of a big map also uses three known ideas, named here with thanks: hop counts are found with a search that switches direction when the frontier gets large (Beamer, Asanovic and Patterson, "Direction-Optimizing Breadth-First Search", 2012); touching items are cleared by first stretching the whole picture and then moving only what still touches, the order used by scale-then-fix overlap removal (Gansner and Hu, "Efficient, Proximity-Preserving Node Overlap Removal", 2010); and the layout stays multilevel, shrinking the map, laying out the small copy and growing it back, as in sfdp (Hu, "Efficient and High Quality Force-Directed Graph Drawing", 2005) and FM3 (Hachul and Juenger, 2004). These four papers are named without links because their pages could not be opened from the build machine on 2026-11-21; no code from them is included. Since 2.6.1 the hop-count table is filled from 64 start items at once, an idea from Then and others, "The More the Merrier: Efficient Multi-Source Graph Traversal" (2014, also named without a link for the same reason). cosmos.gl (cosmosgl/graph, link checked 2026-11-21) was studied as a rival and none of its method is used.
- Related published pieces: netflow-layers and netflow-sankey, both cut from Netflow Map.
Original sources (links checked 2026-11-14; ideas only unless the section above says code is included):
- Layered drawing: Sugiyama, Tagawa and Toda, "Methods for Visual Understanding of Hierarchical System Structures", IEEE SMC, 1981
- Distances (stress) layout by stochastic gradient descent: Zheng, Pawar and Goodman, "Graph Drawing by Stochastic Gradient Descent", 2018
- Force layout far-field: Barnes and Hut, "A hierarchical O(N log N) force-calculation algorithm", Nature, 1986
- Choke points: Cooper, Harvey and Kennedy, "A Simple, Fast Dominance Algorithm", 2001 (a checked formal version: Isabelle AFP entry)