CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-terser

JavaScript parser, mangler/compressor and beautifier toolkit for ES6+

Pending
Overview
Eval results
Files

compression.mddocs/

Compression

Advanced JavaScript code optimization system providing dead code elimination, constant folding, expression simplification, and 75+ individual optimization passes for maximum code size reduction.

Capabilities

Compression Options

Comprehensive optimization configuration with fine-grained control over individual optimization techniques.

interface CompressOptions {
  /** Optimize function arguments and parameters */
  arguments?: boolean;
  /** Convert function expressions to arrow functions when safe */
  arrows?: boolean;
  /** Convert boolean values to integers (true=1, false=0) */
  booleans_as_integers?: boolean;
  /** Optimize boolean expressions and conditions */
  booleans?: boolean;
  /** Collapse single-use variables into their usage sites */
  collapse_vars?: boolean;
  /** Optimize comparison operations (==, !=, <, >, etc.) */
  comparisons?: boolean;
  /** Optimize computed property access to dot notation */
  computed_props?: boolean;
  /** Optimize conditional expressions and ternary operators */
  conditionals?: boolean;
  /** Remove unreachable code (dead code elimination) */
  dead_code?: boolean;
  /** Apply default set of safe optimizations */
  defaults?: boolean;
  /** Remove or optimize directive prologues ("use strict", etc.) */
  directives?: boolean;
  /** Remove console method calls */
  drop_console?: DropConsoleOption;
  /** Remove debugger statements */
  drop_debugger?: boolean;
  /** Target ECMAScript version for optimizations */
  ecma?: ECMA;
  /** Evaluate and fold constant expressions */
  evaluate?: boolean;
  /** Treat input as single expression rather than program */
  expression?: boolean;
  /** Global constant definitions for conditional compilation */
  global_defs?: object;
  /** Hoist function declarations to top of scope */
  hoist_funs?: boolean;
  /** Hoist property assignments out of expressions */
  hoist_props?: boolean;
  /** Hoist var declarations to top of function */
  hoist_vars?: boolean;
  /** Enable Internet Explorer 8 compatibility mode */
  ie8?: boolean;
  /** Optimize if-return statements */
  if_return?: boolean;
  /** Inline function calls and expressions */
  inline?: boolean | InlineFunctions;
  /** Join consecutive var declarations */
  join_vars?: boolean;
  /** Preserve class names during optimization */
  keep_classnames?: boolean | RegExp;
  /** Preserve function arguments in functions marked for removal */
  keep_fargs?: boolean;
  /** Preserve function names during optimization */
  keep_fnames?: boolean | RegExp;
  /** Keep Infinity literal instead of 1/0 */
  keep_infinity?: boolean;
  /** Optimize assignments to left-hand side constants */
  lhs_constants?: boolean;
  /** Optimize loop conditions and structures */
  loops?: boolean;
  /** Treat code as ES6 module */
  module?: boolean;
  /** Convert function expressions to negated IIFEs */
  negate_iife?: boolean;
  /** Number of compression passes to perform */
  passes?: number;
  /** Optimize object property access and assignment */
  properties?: boolean;
  /** Function names safe to remove when return value is not used */
  pure_funcs?: string[];
  /** Constructor functions safe to optimize away */
  pure_new?: boolean;
  /** Property getters that are side-effect free */
  pure_getters?: boolean | 'strict';
  /** Inline and remove function declarations when beneficial */
  reduce_funcs?: boolean;
  /** Optimize variable assignments and reduce variable usage */
  reduce_vars?: boolean;
  /** Join statements with comma operator */
  sequences?: boolean | number;
  /** Drop statements that have no side effects */
  side_effects?: boolean;
  /** Optimize switch statements */
  switches?: boolean;
  /** Apply optimizations to top-level scope */
  toplevel?: boolean;
  /** Variable names to retain in top-level scope */
  top_retain?: null | string | string[] | RegExp;
  /** Optimize typeof expressions */
  typeofs?: boolean;
  /** Enable unsafe arrow function conversions */
  unsafe_arrows?: boolean;
  /** Enable potentially unsafe optimizations */
  unsafe?: boolean;
  /** Unsafe comparison optimizations */
  unsafe_comps?: boolean;
  /** Optimize Function constructor calls (unsafe) */
  unsafe_Function?: boolean;
  /** Optimize Math object method calls (unsafe) */
  unsafe_math?: boolean;
  /** Optimize symbol-related operations (unsafe) */
  unsafe_symbols?: boolean;
  /** Optimize method calls and property access (unsafe) */
  unsafe_methods?: boolean;
  /** Optimize prototype chain operations (unsafe) */
  unsafe_proto?: boolean;
  /** Optimize regular expression literals (unsafe) */
  unsafe_regexp?: boolean;
  /** Optimize undefined comparisons (unsafe) */
  unsafe_undefined?: boolean;
  /** Remove unused variables and functions */
  unused?: boolean;
}

Dead Code Elimination

Comprehensive dead code removal including unreachable statements, unused variables, and side-effect-free expressions.

Usage Examples:

// Aggressive dead code removal
const result = await minify(code, {
  compress: {
    dead_code: true,        // Remove unreachable code
    unused: true,           // Remove unused variables
    side_effects: false,    // Remove side-effect-free statements
    pure_funcs: [           // Functions safe to remove
      'console.log',
      'console.info', 
      'debug'
    ]
  }
});

// Conservative dead code removal
const result = await minify(code, {
  compress: {
    dead_code: true,
    unused: false,          // Keep unused variables (safer)
    side_effects: true      // Preserve all side effects
  }
});

Constant Folding and Evaluation

Compile-time expression evaluation and constant propagation for optimal code reduction.

Usage Examples:

// Enable aggressive constant folding
const result = await minify(code, {
  compress: {
    evaluate: true,         // Fold constant expressions
    conditionals: true,     // Optimize if/else with constants
    booleans: true,         // Optimize boolean expressions
    loops: true,           // Unroll loops with constant bounds
    global_defs: {         // Define compile-time constants
      DEBUG: false,
      API_VERSION: '"1.2.3"',
      MAX_ITEMS: 100
    }
  }
});

// Before: if (DEBUG) { console.log('debug info'); }
// After: (code removed entirely)

Console and Debugging

Control console method handling and debugging statement removal.

Usage Examples:

// Remove all console calls
await minify(code, {
  compress: {
    drop_console: true,
    drop_debugger: true
  }
});

// Selective console removal
await minify(code, {
  compress: {
    drop_console: ['log', 'info'], // Keep console.error, console.warn
    drop_debugger: true
  }
});

// Production vs development builds
const prodOptions = {
  compress: {
    drop_console: true,
    drop_debugger: true,
    pure_funcs: ['console.log', 'assert']
  }
};

const devOptions = {
  compress: {
    drop_console: false,    // Keep console calls
    drop_debugger: false    // Keep debugger statements
  }
};

Multi-pass Optimization

Configure multiple optimization passes for maximum compression with diminishing returns analysis.

Usage Examples:

// Single pass (fastest)
const result = await minify(code, {
  compress: { passes: 1 }
});

// Multiple passes (better compression)
const result = await minify(code, {
  compress: {
    passes: 3,              // 3 optimization passes
    sequences: 200,         // Longer sequence limits
    conditionals: true,     // Multi-pass conditional optimization
    booleans: true         // Multi-pass boolean optimization
  }
});

Unsafe Optimizations

Advanced optimizations that may break code in edge cases but provide significant size reduction.

Usage Examples:

// Conservative unsafe optimizations
const result = await minify(code, {
  compress: {
    unsafe: false,          // Disable most unsafe opts
    unsafe_math: true,      // Only Math optimizations
    unsafe_arrows: false    // Keep original function forms
  }
});

// Aggressive unsafe optimizations
const result = await minify(code, {
  compress: {
    unsafe: true,           // Enable unsafe optimizations
    unsafe_comps: true,     // Unsafe comparisons
    unsafe_Function: true,  // Optimize Function() calls
    unsafe_math: true,      // Optimize Math methods
    unsafe_proto: true,     // Optimize prototype access
    unsafe_regexp: true,    // Optimize RegExp literals
    unsafe_undefined: true  // Optimize undefined usage
  }
});

Types

type ConsoleProperty = keyof typeof console;
type DropConsoleOption = boolean | ConsoleProperty[];

enum InlineFunctions {
  Disabled = 0,
  SimpleFunctions = 1,
  WithArguments = 2,
  WithArgumentsAndVariables = 3
}

type ECMA = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020;

Install with Tessl CLI

npx tessl i tessl/npm-terser

docs

advanced.md

cli.md

compression.md

configuration.md

formatting.md

index.md

mangling.md

minification.md

tile.json