antonjs
antonjs

Reputation: 14318

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.

Upvotes: 10

Views: 11770

Answers (4)

Moji Izadmehr
Moji Izadmehr

Reputation: 2524

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

Upvotes: 2

Kristine P.
Kristine P.

Reputation: 31

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/

Upvotes: 3

parasietje
parasietje

Reputation: 1539

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.

Upvotes: 7

seeming.amusing
seeming.amusing

Reputation: 1179

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.

Upvotes: -7

Related Questions