Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find unused/dead code in web projects (90% code in javascript)

I did find a very interesting tool for identify unused css definitions in a web project. http://www.sitepoint.com/dustmeselectors/

Are there similar tools also for javascript projects?

P.S. I know there is no program for deterministically finding unused code. But I am looking for a report to identify possible unused code. Then the last decision will always be your own.

like image 436
antonjs Avatar asked Feb 21 '12 09:02

antonjs


People also ask

How do you find the 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.

How do I remove unused websites from 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.


1 Answers

Problem is there is no way to be really sure. Suppose the following:

  1. The initial HTML site is practically empty. There is a lot of JS code though, which seems to be unused.
  2. OnLoad, a function is called which launches an AJAX query to the server. The server returns a lot of HTML code, which is the body of the site. This body contains lots of JavaScript functions.
  3. The initial body is replaced with the body received via AJAX. Suddenly, all code is used.

Static analysis utilities are therefore useless. I do not know whether there exists a browser extension that marks all JS usage from a running browser though.

like image 90
parasietje Avatar answered Oct 21 '22 12:10

parasietje