Identify and remove unused foreign functions
Last updated 2 years ago by hcvazquez .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install uffoptimizer 
SYNC missed versions from official npm registry.

UFFO Image


UFFOptimizer is a slimming JavaScript tool for identifying and removing unused foreign functions (UFF).


Example from http://mathjs.org/examples/basic_usage.js.html


Attention: UFFOptimizer only supports ES5!

UFFOptimizer is developed using Node.js execution environment (>= v6.1.0). The following steps are needed for running the tool:

1. Install Node.js environment

Node.js can be downloaded from (https://nodejs.org)

3. Install the UFFOptimizer node module

npm install uffoptimizer -g


1. Go to the path of the project to optimize

If you don't have one, you can try downloading the Math.js experiment example from https://github.com/hcvazquez/ExperimentExample and following the instructions to run a local server.

For Example: cd [project_to_optimize_path]

2. Instrument your js code using the following command

uff instrument_file [file_to_instrument]

For Example: uff instrument_file bundle.js

This step generates a new file, e.g. bundle-instrumented.js

3. Replace original file with instrumented file

To generate profiling info you need to replace in your site the original file with the instrumented file.

For Example: Replace <script src="bundle.js"></script> With <script src="bundle-instrumented.js"></script>

In math.js example:


4. Generate profiling info

You need to run your application and use it. This step print profiling information about used functions into the browser console.

5. Save the browser console output into a file

For this step, you need to open the browser console and save the content into a txt file.

Note: In Chrome, please check that "info" logging level is enable. image

In math.js example:


6. Now, you can use the registered information to optimize your application

How the optimizations works? The optimization removes the UFFs functions from the js file optimized. All the functions removed are listed in a folder created by the tool called "uff" in the same folder in which the optimized file is located. To avoid potential runtime errors owing to functions removed wrongly, UFFOptimizer replace the functions with an AJAX synchronous call that dinamically load the function from the server in case of need it.

Note: The file to optimize needs to be the original file.

You can optimize your original file as follow.

uff optimize_file_browser [file_to_optimize] [profiling_file]

For Example:

uff optimize_file_browser bundle.js profiling.txt

This step generates a new file, e.g. bundle-optimized.js

7. Test your optimization file

To test your optimized file you need to replace in your site the original file with the optimized file.

For Example:


<script src="bundle.js"></script>


<script src="bundle-optimized.js"></script>

VERY IMPORTANT! Additionally, in the place where the file was optimized, the optimizer has created an "uff" folder with all optimized functions inside it. You also need to deploy that folder on the server so that the asynchronous load can find the functions.

Note: Please check that the application has access to the "uff" folder. The ajax call will try to load the functions from the root. The path to the file look like this: $dl ('uff/$_-7697924661507122750048.js').

In math.js example:


Also you can test the UFFs that were cropped from the bundle.

For example, in the math.js experiment you can try (in your page, or in the browser developer console):

math.multiply(math.eye(1000, 1000, 'sparse'), math.eye(1000, 1000, 'sparse'));

You should not see any error.

If you want to see that functions were loaded lazily, you must put in the browser developer console the code:



Instrument in a Production Environment

To generate log information in a production environment UFFOptimizer uses google analytics and registers the use of functions as events.

You need to run the next command:

uff instrument_file_ga [file_to_optimize]

And, change in your Google Analytics code snippet the variable name 'ga' to '_$ga'

We decided change the variable name to avoid conflicts with other variables with the same name.

An example of GA code snippet is the following

      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      _$ga('create', 'YOUR CODE', 'auto');//your code example UA-132123123-1
      _$ga('send', 'pageview');

Note: In the previous example '_$ga' appears three times.

Current Tags

  • 1.0.16                                ...           latest (2 years ago)

17 Versions

  • 1.0.16                                ...           2 years ago
  • 1.0.15                                ...           2 years ago
  • 1.0.14                                ...           2 years ago
  • 1.0.13                                ...           2 years ago
  • 1.0.12                                ...           2 years ago
  • 1.0.11                                ...           2 years ago
  • 1.0.10                                ...           2 years ago
  • 1.0.9                                ...           2 years ago
  • 1.0.8                                ...           2 years ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 0
Last Month 17
Dependencies (10)
Dev Dependencies (20)
Dependents (0)

Copyright 2014 - 2016 © taobao.org |