10

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.

1
  • Bust out the breakpoints and start running Selenium. Commented Feb 21, 2012 at 9:35

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I agree, the program cannot deterministically find unused code. But I am looking for a report to identify possible unused code. Then the last decision will always be your own.
Read stackoverflow.com/questions/53249/… to find a tool which marks all code that has been run in your browser. It will allow you to easily identify which code can possibly be dead.
Since Chrome 59, you can use the coverage dev tools to help you identify the dead code in a running web app.
3

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

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

1 Comment

Please, provide concrete snippet that solves the problem, not the link.
2

In order to find the unused assets, to remove manually, you can use deadfile library: https://m-izadmehr.github.io/deadfile/

It can simply find unused files, in any JS project.

Without any config, it supports ES6, JSX, and Vue files: enter image description here

Comments

-7

The one that comes to mind most quickly is Javascript LINT (http://www.javascriptlint.com/) and JSLint (http://www.jslint.com/).

Beware though: the latter hurts your feelings.

2 Comments

Those test for coding practises that some people consider to be poor. They don't test for unused code.
Maybe it's time to delete this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.