Benchmark from lazutkin.com, the companion to Duff's device in JavaScript.
The follow-up, Duff's device, part 2: copying within an array,
asks the same question of a range copied inside one array, where
Array#copyWithin competes; its benchmark is in
a separate gist.
bench-duff.mjs— the four contenders, all copying 127 elements from one array into another: the simple loop;unrolled-loop, unrolled by ten with a second loop for the remainder;duff-device, unrolled by ten with a fall-throughswitchfor the remainder; andduff-device-nested, one loop stepping by ten where aswitchon the remaining count decides how much of the block runs.
npm install
npm run bench
The harness is nano-bench; its Concepts page explains how the confidence intervals and the significance tests are computed. Defaults are 100 samples of 50 ms per function.
The work is CPU-bound, so run one process at a time and one machine at a time. Results move with the engine, the engine version and the CPU, so a number from a single box says nothing on its own.
Update 2026-09-13. A fifth contender, duff-device-nested-default, is duff-device-nested with default: in place of its case 10: label. It reproduces the correction in the post's P.S.: the label, not the dispatch, is what made the nested device run at about twice the flat-tail version (on the i3-10110U under Node 26.8.2: 208 ns with the label, 121 without, 107 for duff-device; on an i9-11900K: 138, 70, 69). The original four contenders are unchanged.