SSJX.CO.UK
Content

Reduce JavaScript file size with Crunch

Introduction

Crunch is a simple tool that reduces the size of (some) JavaScript files by removing comments and new lines. This process can break some JavaScript files so there is also a 'check' mode that looks for possible problems and offers suggestions on improvements.

The advantage of smaller JavaScript files is they are quicker to download, this can improve website performance and may help reduce bandwidth costs! Those reasons are enough to warrant giving this program a try!

Download for Windows (32bit)

To keep the download size small, this archive is provided as a 7zip file. The archive includes the compiled exe and D Language source code.

⚠️ If you get a message about missing msvcr120.dll or vcruntime140.dll (or similar), you need either the Visual C++ Runtime 2013 or Visual C++ Runtime 2015-2022 from Microsoft installed. You can get them from here. The programs on this page are all 32bit so you will need that runtime even if you are running 64bit.

Worked Example

This is our JavaScript test file which we will call test.js.

let a=[
1,2,3,4,
]

function hello(){
	let a=1
	var website="http://ssjx.co.uk";
	console.log("hello world");	/// Prints hello
}
	
let comment="/* */"

function world(){
	console.log("hello again");	// Prints hello
}

Checking For Possible Problems

To check the file we add the '-check' switch:

c:\temp>crunch -check test.js
JS Crunch by ssjx (https://ssjx.co.uk) v0.1 (20/03/2025)
===========================================
Possible error line 6 : let a=1
1 possible errors found.

So we need to add a semi colon. Note that it did not complain about the lack of a semi colon on line 3 or 11, these will get added automatically.

Compressing The JavaScript File

Once we have fixed the above, we run the program again with no switches.

c:\temp>crunch test.js
"use strict";let a=[1,2,3,4,];
function hello(){let a=1;string website="http://ssjx.co.uk";console.log("hello world");}let comment="/* */";
function world(){console.log("hello again");}

To create a new file we can redirect the output.

c:\temp>crunch test.js >compressed.js

Basic Mode

As mentioned above, when compressing, semi-colons may get added. It is possible that these get added when they are not wanted. Using the switch '-basic' enable basic mode which removes comments and newlines and does nothing else:

c:\temp>crunch -basic test.js
"use strict";let a=[1,2,3,4,]function hello(){let a=1string website="http://ssjx.co.uk";console.log("hello world");}let comment="/* */"function world(){console.log("hello again");}

The above would likely not work due to the missing semi-colons.

Conclusion

Admittedly, the Google Closure compiler is better and can produce smaller files but this one is fast and easy to use with no dependencies. Using this program reduced the Stixx game's JavaScript size from 24KB to 14KB!

Created 21/03/24