AngryHacker
AngryHacker

Reputation: 61606

Is there an effective way to find which JavaScript includes are not used?

I've inherited a page that is extremely complex and has a ton of <script src=... includes.

I suspect a lot of the includes are not used. Is there a way to figure out which of them are not used?

Upvotes: 9

Views: 129

Answers (1)

Scott
Scott

Reputation: 5379

Chrome 59 includes a new Coverage tool, which can be enabled from the 3-dot menu in DevTools.

Chrome DevTools -> Options -> More tools -> Coverage

You should be able to enable the tool, navigate the website and perform scripted actions, and then view which lines were actually called. Files that are not called will appear entirely red (no lines run).

Edit: As mentioned in the comments, this is still not an optimal solution, since it will only detect lines which have actually run. As @adeneo mentioned, it is almost impossible to statically determine which parts of the code will run, simply because of the complexity of JS.

If this is an XY Problem actually intended to decrease the number of initial HTTP requests, it may be a good idea to simply concatenate all required files and minify that (HTTP/1.1), or investigate grouping related assets and serving via HTTP/2.

Upvotes: 10

Related Questions