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

I found a very interesting tool for defining unused CSS definitions in a web project. http://www.sitepoint.com/dustmeselectors/

Are there similar tools for javascript projects?

PS I know that there is no program for the deterministic search for unused code. But I'm looking for a report to identify possible unused code. Then the last decision will always be yours.

+8
javascript code-coverage
source share
3 answers

The problem is that there is no way to be really sure. Assume the following:

  • The original HTML site is almost empty. However, there is a lot of JS code that doesn't seem to be used.
  • OnLoad, a function is called that launches an AJAX request to the server. The server returns a lot of HTML code, which is the body of the site. This body contains many JavaScript features.
  • The original body is replaced by the body received through AJAX. Suddenly all code is used.

Static analysis utilities are therefore useless. I don't know if there is a browser extension that marks all JS usage from a working browser.

+5
source share

You can try using tombstones to safely find and remove dead code from your JavaScript.

https://blog.bugsnag.com/javascript-refactoring-with-bugsnag-and-tombstones/

+2
source share

Javascript LINT ( http://www.javascriptlint.com /) and JSLint ( http://www.jslint.com/ ) come to mind most quickly.

Beware: the latter hurts in your feelings.

-5
source share

All Articles