Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove dead code from Javascript [duplicate]

Tags:

I am trying to remove unused functions from my project. Since it has thousands of lines, this takes forever.

Code coverage tools may suggest functions that are not used in a given test case, but it may be dangerous to rely only on this.

Is there another tool which can help with this?

like image 773
Yuvals Avatar asked Jul 18 '11 08:07

Yuvals


People also ask

How do I remove unused JavaScript?

If your website is running on WordPress, you can remove the unused JavaScript from its pages using special plugins. For example, you can use AssetCleanUp, which also allows you to disable unused JavaScript files. Another option is to detect unused JS with Chrome DevTools and delete unnecessary files.

Should unused code be removed?

Here are some reasons why unused code should be removed: For anyone new working on a project, they not only have to understand the working code, they have to understand unused material also. This is wasted time and creates confusion.

How do we refactor dead code?

The quickest way to find dead code is to use a good IDE. Delete unused code and unneeded files. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used. To remove unneeded parameters, use Remove Parameter.


2 Answers

You can try Google Closure compiler. It has different settings and you can also remove dead code automatically. You can try it here: http://closure-compiler.appspot.com/home

For more info read here: http://code.google.com/closure/compiler/docs/compilation_levels.html

like image 175
daveoncode Avatar answered Oct 02 '22 14:10

daveoncode


Have a look at this question regarding Javascript static analysis tools.

like image 42
Nico Huysamen Avatar answered Oct 02 '22 12:10

Nico Huysamen