JS-modulesnetflow-engine
2.7.0 · API 2

netflow-engine

The engine of Netflow Map as a module you can copy and adapt: nine views of a map and eleven network layouts, no two items on top of each other, WebAssembly twins for Force and Distances, and a use() hand-over for the layers and Sankey add-ons; a 30,000-item network lays out in about 0.8 s in the Netflow Map app (Firefox).

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.

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

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:

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.

ItemsLibraryungoogled-chromiumFirefoxWebKitGTKQuality
300Netflow engine113 ms93 ms104 ms0.178
300d3-force377 ms450 ms370 ms0.194
300Cytoscape (cose)2,361 ms3,943 ms2,880 ms0.167
1,000Netflow engine393 ms387 ms379 ms0.171
1,000d3-force1,505 ms1,768 ms1,429 ms0.194
1,000Cytoscape (cose)28.6 s (1 run)over 60 sover 60 s0.167
3,000Netflow engine1,399 ms1,462 ms1,584 ms0.190
3,000d3-force4,992 ms6,023 ms5,101 ms0.198
3,000Cytoscape (cose)over 60 sover 60 sover 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.

ItemsLibraryungoogled-chromiumFirefoxWebKitGTKQuality
300Netflow engine55 ms43 ms34 ms0.161
300ELK22.8 s (1 run)26.4 s (1 run)15.9 s (1 run)0.170
1,000Netflow engine233 ms209 ms228 ms0.159
1,000ELKover 60 sover 60 sover 60 s-
3,000Netflow engine1,367 ms1,323 ms1,123 ms0.172
3,000ELKover 60 sover 60 sover 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.

ItemsLibraryungoogled-chromiumFirefoxWebKitGTKQuality
300Netflow engine38 ms28 ms17 ms559
300dagre369 ms346 ms455 ms784
300ELK714 ms500 ms862 ms632
1,000Netflow engine68 ms44 ms32 ms6,499
1,000dagre5,927 ms4,445 ms7,786 ms6,885
1,000ELK1,827 ms2,417 ms2,142 ms6,471
3,000Netflow engine116 ms91 ms80 ms37,247
3,000dagreover 60 sover 60 sover 60 s-
3,000ELK8,404 ms12.6 s (1 run)7,688 ms35,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.

This engine 2.0.0 (MIT), layout only, readable source with WebAssembly
Gzipped (smaller is better)
195,252 bytes (not minified)61,043 bytes
d3 7.9.0 (ISC), whole bundle; d3-force is one part
Gzipped (smaller is better)
279,706 bytes92,370 bytes
Cytoscape 3.34.4 (MIT), also draws and edits graphs
Gzipped (smaller is better)
435,649 bytes136,431 bytes
dagre 0.8.5 (MIT)
Gzipped (smaller is better)
283,803 bytes64,833 bytes
ELK (elkjs) 0.12.0 bundled (EPL-2.0 or GPL-3.0-or-later)
Gzipped (smaller is better)
1,609,707 bytes466,978 bytes

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):

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):

MapItems2.6.52.7.0Best rival
SBA3000.1620.146 to 0.148Cytoscape 0.144 to 0.148
SBA9220.1840.165 to 0.167Cytoscape 0.170
SEC3000.230 to 0.2520.187d3-force 0.217
SEC1,0000.184 to 0.1910.174d3-force 0.186
SEC2,6830.167 to 0.1690.175d3-force 0.187
Retail3,0000.215 to 0.2210.201 to 0.203d3-force 0.219
FHIR3,0000.178 to 0.1790.177d3-force 0.180
Retail, FHIR300, 1,0000.175 to 0.178samed3-force 0.176 to 0.178

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.

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

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:

1,000 items
Chromium 155.0.8059.39192 ms
Firefox 158.0240 ms
WebKitGTK 2.52.6244 ms
3,000 items
Chromium 155.0.8059.39924 ms
Firefox 158.0854 ms
WebKitGTK 2.52.6770 ms
10,000 items
Chromium 155.0.8059.39746 ms
Firefox 158.0810 ms
WebKitGTK 2.52.6552 ms

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
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
  1. The Netflow Map team finishes an engine change and writes a short note (what changed, in numbers, from the three test browsers).
  2. 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.mjs in Netflow Map, so the two cannot drift apart.
  3. 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

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