tags:

views:

713

answers:

3

I've got a collection of javascript files from a 3rd party, and I'd like to remove all the unused methods to get size down to a more reasonable level.

Does anyone know of a tool that does this for Javascript? At the very least give a list of unused/used methods, so I could do the manually trimming? This would be in addition to running something like the YUI Javascript compressor tool...

Otherwise my thought is to write a perl script to attempt to help me do this.

+11  A: 

No. Because you can "use" methods in insanely dynamic ways like this.

obj[prompt("Gimme a method name.")]();
Triptych
Yeah, the best you could do is a rough pass.
Nosredna
I'm going to start using this method on all my projects.
tj111
Wow. That's NASTY!
Alan
Nasty *awesome*, you mean.
Chuck
but barring the example and other user-generated/random-generated methods, couldn't you whittle down the files?
jedierikb
@jedierikb - No. There's no reliable way to determine exactly which code will run.
Triptych
+2  A: 

Unless the library author kept track of dependencies and provided a way to download the minimal code [e.g. MooTools Core download], it will be hard to to identify 'unused' functions.

The problem is that JS is a dynamic language and there are several ways to call a function.

E.g. you may have a method like

function test() 
{
   //
}

You can call it like

   test();

   var hello = i > 1 ? 'test' : 'xyz';
   hello();

   window[hello]();
SolutionYogi
+1  A: 

Check out JSCoverage . Generates code coverage statistics that show which lines of a program have been executed (and which have been missed).

foson