Three years ago, I made a post about minifying Elm code:
Now I’m back on that topic again! This time, I created a tool for benchmarking different minifiers against each other, on a compiled Elm JS file of choice:
The winner?
If you use SWC with the settings from the Elm Guide, you seem to get about the smallest size in very little time. It’s not the fastest, but still not even close to a second even on a large Elm app (depending on the computer of course).
import * as swc from "@swc/core";
const pureFuncs = [ "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"];
async function minify(code) {
return (
await swc.minify(code, {
compress: {
pure_funcs: pureFuncs,
pure_getters: true,
unsafe_comps: true,
unsafe: true,
},
mangle: {
reserved: pureFuncs,
},
})
).code;
}
Example
❯ node bench.js example-azimutt.js
┌─────────┬───────────────────────┬───────────┬─────────────┬───────┬───────────────┬─────────┬───────────────┬─────────┬───────────────────────────────────────────────────────┐
│ (index) │ name │ version │ time │ x │ size │ % │ brotli ⬇ │ % │ installation size and dependencies │
├─────────┼───────────────────────┼───────────┼─────────────┼───────┼───────────────┼─────────┼───────────────┼─────────┼───────────────────────────────────────────────────────┤
│ 0 │ '(none)' │ '' │ '' │ '' │ ' 1.35 MiB' │ '' │ ' 167 KiB' │ '' │ '' │
│ 1 │ '@swc/core_elm-guide' │ '1.7.26' │ ' 196 ms' │ 'x6' │ ' 411 KiB' │ '-70 %' │ '🏆 101 KiB' │ '-40 %' │ 'https://packagephobia.com/result?p=@swc/core' │
│ 2 │ 'uglify-js_elm-guide' │ '3.19.3' │ ' 2.43 s' │ 'x78' │ '🏆 402 KiB' │ '-71 %' │ ' 102 KiB' │ '-39 %' │ 'https://packagephobia.com/result?p=uglify-js' │
│ 3 │ '@swc/core' │ '1.7.26' │ ' 198 ms' │ 'x6' │ ' 410 KiB' │ '-70 %' │ ' 102 KiB' │ '-39 %' │ 'https://packagephobia.com/result?p=@swc/core' │
│ 4 │ 'terser_elm-guide' │ '5.32.0' │ ' 1.64 s' │ 'x53' │ ' 416 KiB' │ '-70 %' │ ' 103 KiB' │ '-39 %' │ 'https://packagephobia.com/result?p=terser' │
│ 5 │ 'uglify-js' │ '3.19.3' │ ' 2.29 s' │ 'x74' │ ' 405 KiB' │ '-71 %' │ ' 103 KiB' │ '-38 %' │ 'https://packagephobia.com/result?p=uglify-js' │
│ 6 │ 'uglify-js+esbuild' │ '' │ ' 1.75 s' │ 'x56' │ ' 403 KiB' │ '-71 %' │ ' 104 KiB' │ '-38 %' │ '' │
│ 7 │ 'terser' │ '5.32.0' │ ' 1.34 s' │ 'x43' │ ' 419 KiB' │ '-70 %' │ ' 104 KiB' │ '-38 %' │ 'https://packagephobia.com/result?p=terser' │
│ 8 │ 'esbuild_tweaked' │ '0.23.1' │ ' 61 ms' │ 'x2' │ ' 423 KiB' │ '-69 %' │ ' 107 KiB' │ '-36 %' │ 'https://packagephobia.com/result?p=esbuild' │
│ 9 │ 'esbuild' │ '0.23.1' │ ' 56 ms' │ 'x2' │ ' 430 KiB' │ '-69 %' │ ' 110 KiB' │ '-34 %' │ 'https://packagephobia.com/result?p=esbuild' │
│ 10 │ 'bun' │ '1.1.27' │ '🏆 31 ms' │ 'x1' │ ' 433 KiB' │ '-69 %' │ ' 110 KiB' │ '-34 %' │ 'https://packagephobia.com/result?p=bun' │
│ 11 │ '@tdewolff/minify' │ '2.20.37' │ ' 48 ms' │ 'x2' │ ' 434 KiB' │ '-69 %' │ ' 114 KiB' │ '-32 %' │ 'https://packagephobia.com/result?p=@tdewolff/minify' │
└─────────┴───────────────────────┴───────────┴─────────────┴───────┴───────────────┴─────────┴───────────────┴─────────┴───────────────────────────────────────────────────────┘