Created
August 9, 2026 23:39
-
-
Save uhop/97773df567141930eef9bc7caada915b to your computer and use it in GitHub Desktop.
Duff's device in JavaScript — benchmark from lazutkin.com. Run: npm install && npm run bench
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const SIZE = 127; | |
| export const source = new Array(SIZE); | |
| export const target = new Array(source.length); | |
| for (let i = 0; i < source.length; ++i) { | |
| source[i] = i; | |
| } | |
| export default { | |
| 'simple-loop': n => { | |
| for (let i = 0; i < n; ++i) { | |
| for (let j = 0; j < source.length; ++j) { | |
| target[j] = source[j]; | |
| } | |
| } | |
| }, | |
| 'unrolled-loop': n => { | |
| for (let i = 0; i < n; ++i) { | |
| let j = 0; | |
| for (; j + 10 <= source.length; j += 10) { | |
| target[j] = source[j]; | |
| target[j + 1] = source[j + 1]; | |
| target[j + 2] = source[j + 2]; | |
| target[j + 3] = source[j + 3]; | |
| target[j + 4] = source[j + 4]; | |
| target[j + 5] = source[j + 5]; | |
| target[j + 6] = source[j + 6]; | |
| target[j + 7] = source[j + 7]; | |
| target[j + 8] = source[j + 8]; | |
| target[j + 9] = source[j + 9]; | |
| } | |
| for (; j < source.length; ++j) { | |
| target[j] = source[j]; | |
| } | |
| } | |
| }, | |
| 'duff-device': n => { | |
| for (let i = 0; i < n; ++i) { | |
| let j = 0; | |
| for (; j + 10 <= source.length; j += 10) { | |
| target[j] = source[j]; | |
| target[j + 1] = source[j + 1]; | |
| target[j + 2] = source[j + 2]; | |
| target[j + 3] = source[j + 3]; | |
| target[j + 4] = source[j + 4]; | |
| target[j + 5] = source[j + 5]; | |
| target[j + 6] = source[j + 6]; | |
| target[j + 7] = source[j + 7]; | |
| target[j + 8] = source[j + 8]; | |
| target[j + 9] = source[j + 9]; | |
| } | |
| switch (source.length - j) { | |
| case 9: target[j + 8] = source[j + 8]; | |
| case 8: target[j + 7] = source[j + 7]; | |
| case 7: target[j + 6] = source[j + 6]; | |
| case 6: target[j + 5] = source[j + 5]; | |
| case 5: target[j + 4] = source[j + 4]; | |
| case 4: target[j + 3] = source[j + 3]; | |
| case 3: target[j + 2] = source[j + 2]; | |
| case 2: target[j + 1] = source[j + 1]; | |
| case 1: target[j] = source[j]; | |
| } | |
| } | |
| }, | |
| 'duff-device-nested': n => { | |
| for (let i = 0; i < n; ++i) { | |
| for (let j = 0; j < source.length; j += 10) { | |
| switch (Math.min(source.length - j, 10)) { | |
| case 10: target[j + 9] = source[j + 9]; | |
| case 9: target[j + 8] = source[j + 8]; | |
| case 8: target[j + 7] = source[j + 7]; | |
| case 7: target[j + 6] = source[j + 6]; | |
| case 6: target[j + 5] = source[j + 5]; | |
| case 5: target[j + 4] = source[j + 4]; | |
| case 4: target[j + 3] = source[j + 3]; | |
| case 3: target[j + 2] = source[j + 2]; | |
| case 2: target[j + 1] = source[j + 1]; | |
| case 1: target[j] = source[j]; | |
| } | |
| } | |
| } | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "duff-device-bench", | |
| "private": true, | |
| "type": "module", | |
| "scripts": { | |
| "bench": "nano-bench bench-duff.mjs" | |
| }, | |
| "devDependencies": { | |
| "nano-benchmark": "^1.2.0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment