33 releases

0.6.0 Dec 25, 2023
0.5.6 Feb 14, 2023
0.4.3 Jan 31, 2023
0.4.0 Nov 3, 2022
0.1.5 Jun 23, 2022

#132 in Compression

Download history 3625/week @ 2024-07-21 4171/week @ 2024-07-28 3634/week @ 2024-08-04 3862/week @ 2024-08-11 3941/week @ 2024-08-18 4027/week @ 2024-08-25 3664/week @ 2024-09-01 3781/week @ 2024-09-08 3767/week @ 2024-09-15 3897/week @ 2024-09-22 6011/week @ 2024-09-29 5673/week @ 2024-10-06 5372/week @ 2024-10-13 6574/week @ 2024-10-20 6365/week @ 2024-10-27 6174/week @ 2024-11-03

24,970 downloads per month
Used in 52 crates (14 directly)

Apache-2.0

105KB
2.5K SLoC

minify-js

Extremely fast JavaScript minifier, written in Rust.

Goals

  • Fully written in Rust for maximum compatibility with Rust programs and derivatives (FFI, WASM, embedded, etc.).
  • Maximises performance on a single CPU core for simple efficient scaling and easy compatible integration.
  • Minification of individual inputs/files only; no bundling or transforming.
  • Prefer minimal complexity and faster performance over maximum configurability and minimal extra compression.

Performance

Comparison with esbuild, run on common libraries.

Chart showing speed of JS minifiersChart showing compression of JS minifiers

Features

  • Fast parsing powered by SIMD instructions and lookup tables.
  • Data is backed by a fast reusable bump allocation arena.
  • Supports JSX.
  • Analyses scopes and variable visibilities.
  • Minifies identifiers.
  • Omits semicolons, spaces, parentheses, and braces where possible.
  • Transforms functions to arrow functions when new, this, arguments, and prototype aren't used.
  • Transforms if statements to expressions.

Usage

CLI

Precompiled binaries are available for Linux, macOS, and Windows.

Linux x64 | macOS x64 | Windows x64

Use the --help argument for more details.

minify-js --output /path/to/output.min.js /path/to/src.js

Rust

Add the dependency:

[dependencies]
minify-js = "0.6.0"

Call the method:

use minify_js::{Session, TopLevelMode, minify};

let mut code: &[u8] = b"const main = () => { let my_first_variable = 1; };";
let session = Session::new();
let mut out = Vec::new();
minify(&session, TopLevelMode::Global, code, &mut out).unwrap();
assert_eq!(out.as_slice(), b"const main=()=>{let a=1}");

Node.js

Install the dependency:

npm i @minify-js/node

Call the method:

import {minify} from "@minify-js/node";

const src = Buffer.from("let x = 1;", "utf-8");
const min = minify(src);

In progress

  • Combine and reorder declarations.
  • Evaluation and folding of constant expressions.
  • Parse and erase TypeScript syntax.
  • Removal of unreachable, unused, and redundant code.
  • Inlining single-use declarations.
  • Replacing if statements with conditional and logical expressions.
  • Returning an explicit error on illegal code e.g. multiple declarations/exports with identical names.
  • Much more inline, high level, and usage documentation.
  • Support import and export string names e.g. import { "a-b" as "c-d" } from "x".
  • Simplify pattern parsing and minification.
  • Micro-optimisations:
    • Unwrap string literal computed members, then identifier or number string members.
    • Replace x === null || x === undefined with x == null, where x is side-effect free.
    • Replace typeof x === "undefined" with x === undefined.
    • Using shorthand properties.
    • Replace void x with x, undefined.
    • Replace return undefined with return.
    • Replace const with let.
    • Hoist let and const.
    • Unwrapping blocks.
    • Unwrapping paretheses, altering expressions as necessary.
    • if (...) return a; else if (...) return b; else return c => return (...) ? a : (...) ? b : c.

Dependencies

~3.5MB
~52K SLoC