node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org ยป


Unsafe Minifier

Note: This is a prototype, the code quality and test coverage will significantly increase as I move away from the prototype.

Welcome to the world's first unsafe minifier (at least I think). Unlike traditional (safe) minifiers this minifier attempts to modify the flow of code but not the fundamental function of the code. It is important to note that due to the inclusion of AI, this minification technique will probably never be 100% accurate on choosing what should be minified. Although, over time I intend to get the accuracy about 90%. Finally, this minifer is intented to be used in combination with safe minification to provide levels of minification not possible by safe minification alone.

The code currently runs as a CLI accepting a file to be minified, outputting four files:

  • File minified just by the traditional minifier (Uglify.js).
  • file_name.min.js: File unsafely minified.
  • file_name.full.min.js: File unsafely minified.

These four outputs should give a clear picture of the limits and potential of the current version of unsafe minification. Additonally, I have included a simple file in the top directory of the project demonstrating an optimal case of minification.

An analysis and summary of the decisions made can be found in Report.pdf.


These installation instructions are intended for a Debian based OS, though they should apply to most UNIX based OSs.

Automatic (Debian OS only):

  • Run: make install

Dependencies (automatically resolved with the automatic installer):

  • For NVM:
  • build-essential
  • libssl-dev
  • curl
  • git-core
  • For Node.js:
  • python
  • g++
  • make
  • For safe minification: UglifyJS (NPM module)


  1. Install Node.js v0.8.2, not the latest as there is a problem with the node fann bindings library.
  • I suggest using nvm (Node Version Manager) to install Node.js.
  1. Install Uglify.js globally: npm install uglify-js -g
  2. Installing FANN (Fast Artificial Nueral Networks):
  1. If running "node main.js" results in an error, run 'ldconfig'. The exact command may vary based on OS.

Contact me at if any of these steps don't work on your machine.

Structure Of The Application

Below is an explanation of the main files and directories in this directory. Details on specific directory structure can be found in respective READMEs.


  • AST_modification: Code to transform ASTs, mostly function merging. Includes statistics generation.
  • data: where raw, transformed, and statistical data is contained.
  • generation: Code used to create data for that the training section trains on.
  • minification: Code used to minify a sent in file.
  • node_modules: Libraries that were used in my code are stored here.
  • training: Code to train and save the nueral networks.
  • tests: Tests for all the code, can be run from the top level with: make test-all


  • app.js: Central file that runs everything.
  • Bash script that runs the installation process automatically.
  • Makefile Defines the commands to test this application.
  • messages.js: Central location of all error and console messages.
  • MIT_License: Document explaining this project's MIT license, nothing special added.
  • report.pdf: Report on why I made the choices I did and some of the data behind those choices. This is now becoming out of date as it was done at the start of the project.
  • simple_example.js: A two function file with 2 calls that can be minified, same example as in report.
  • simple_example.min.js: The simple example unsafely minified.
  • simple_example.full.min.js: The simple example unsafely minified and then safely minified.
  • The simple example just safely minified.
  • utility.js: A bunch of functions I wrote (except the custom array remove) for use in the entire program.


To run all tests: make test-all or make test-a

Debugging Tests: One debug listener for unit and integration tests

  1. Start the listener: make test-d or make test-debug
  2. Start the inspector: node-inspector
  3. Debug in Chrome: Go to the listed page in Chrome (and only Chrome), probably

Unit Tests: Covers all lower level code.

  • Run them once: make test or make test-u or make test-unit
  • Run them with every code change: make test-w or make test-watch

Integration Tests: Covers the three main sections of the code; data generation, machine learning, code minification.

  • Run them once: make test-i or make test-integration
  • Generator tests:
    • All files in data/raw_data ending in "test" will be used.
    • Test by writing two functions to be merged then specify what the result code should be:

function1 (a){ console.log(a); }
function2 (b){ return b * 2; }

var b = function2(b);

function double(a, b) {
return b * 2;
var b = double('hello', b);

All parts of the code are to be tested although to different degrees. Low level code will have unit tests (in a behavioral style) like in the current utility_function.js tests. The test code is located in the test directory and is driven by the index.js file there. Tests are run by a Makefile which specifies mocha to test in a certian way.


The goal is to upgrade the minifier to the point where it can correct choose above 90% of minifications and where it provides a minification bonus of 50%.

Short Term: Test and improve function finding in the AST analysis.


Apart from unit tests for function finding in ASTs the project is fully tested so contributions are now welcome.