JSlite: PHP Javascript Minifier

JSlite LogoA Javascript compiler designed for minifying inline scripts, written in PHP.

Project Description

Designed to complement HTMLdoc, JSlite is a Javascript compiler and minifier, designed for minifying inline JavaScript on the fly. It can also be used for compressing larger documents.

The software is implemented as a compiler to ensure reliable, and comes with a full test suite.

How JSlite Works

Under the hood, the software processes are split into a number of stages:


The input JavaScript is loaded into the tokeniser as a string, and a regular expression splits it up into categorised tokens.


The tokens are passed to the parser which then loops through and consumes each token through an object based Finite State Machine to create an internal object structure that represents the document. This enables irregular tokens to be ignored and the document to be parsed with more reliability.

Once parsed, the AST will contain an array of child objects each representing each expression, variable, and structure, which will in turn have their own child expressions, functions and variables.


Minification is performed by each object on its own structure, the command passed down each level from the one above.


The compilation process reconstructs the JavaScript from its object representation. Each object generates itself as a string, and then requests its children generate themselves. The result is all concatenated together and either output as a string or saved to the requested location.


JSlite has been written with performance in mind, as its main purpose is for the minification of inline JavaScript. Since PHP is widely used to generate HTML that may contain inline JavaScript, it is expected and designed for the use case that minification will happen on the fly.

For most JavaScript documents the speed and memory usage will be well within acceptable boundaries, although if you put large complex documents into it with a large amount of nodes, the speed will suffer.

For the most part the object is memory efficient, most memory will be used by the tokenisation process, which uses a regular expression to split the input string into tokens. This part of the program has been optimised to minimise the amount of memory used.

You can try out JSlite in my apps.