Benchmark from lazutkin.com, the companion to Duff's device, part 2: copying within an array, which follows Duff's device in JavaScript.
The first post copied one array into another; its benchmark is in
a separate gist.
This one copies a range within one array, which is Array#copyWithin's
domain, so the built-in and the hand-written loops race on equal terms.
copy-within.js— the four contenders on a packed array: the simple loop,duff-device-1(unrolled by ten plus aswitchfor the remainder),duff-device-3(one loop, theswitchdecides how much of the block runs) andArray#copyWithin. All copy the same 127 elements from the front half of a 255-element array into the back half; the ranges do not overlap.copy-within-holey.js— the samecopyWithincall on a packed array and on a holey one with identical contents.simple-loop-holey.js— the control for the file above: the same packed-vs-holey question asked of the simple loop.
npm install
npm run bench # the four contenders
npm run bench:holey # copyWithin, packed vs holey
npm run bench:control # the simple loop, packed vs holey
npm run bench:deno # the four contenders under Deno
npm run bench:bun # the four contenders under Bun
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.