jsjam - Compress JavaScript code


jsjam - Compress JavaScript code.


jsjam [opts] file...


-d --debug             Debug mode.
-h --help              Print help and exit.
-q --quiet             Quiet mode.
-v --version           Print version and exit.
-i --keep-identifiers  Do not shorten identifiers.
-g --keep-globals      Do not shorten non-"var" identifiers.
-w --keep-whitespace   Do not compress whitespace.
-n --keep-newlines     Do not remove newlines following stuff.
-c --keep-comments     Do not compress comments (implies -n).
-a --add-note N        Adds note "N" to the end of the compressed
output (in a // comment).
The options --keep-identifiers and --keep-globals are not compatible.


file           One or more JavaScript files to compress.  If no files
are specified, stdin is used.


The jsjam Perl script is available here:


This program attempts to compress JavaScript so that it downloads faster to the browser. If the identifier compression is kept on, it also has a side-effect of making the JavaScript fairly unreadable.

The compressed output for all input files is sent to stdout.

Compression methods include:

- Strip comments.
- Strip unnecessary whitespace.
- Shorten identifiers (variable names, function names, field names).

Identifiers which should not be shortened can be indicated in the JavaScript code using one line per identifier in the form:


where ``identifier'' is the identifier which should not be shortened.


jsjam --debug mycode.js >mycode-jsjam.js


This program was originally written to compress one particular set of JavaScript software, but many others have found it useful for their situations as well. If this happens to work or almost work for you, please drop a note to the author. Bug reports are welcomed and may even get fixed, especially if you can provide sample JavaScript code that illustrates the problem.

If you have any short global variable names (1-2 characters), the --keep-globals option will probably shorten local (var) variables so that they conflict with the global variables.

The characters // may be treated as the start of a comment even if they are inside a string. To get around this, replace

var url = "http://site/page/";


var url = "http:/"+"/site/page/";

In addition to working well on Linux/Unix, this script has reportedly been able to run on Perl under Windows, though some Windows users report that it is better to remove the first line of this script file (#!/usr/bin/perl -wT)


Eric Hammond <>

Copyright ©1998-2015
Eric Hammond