Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect linked & unused files and unused JavaScript

Tags:

I just finished my website, which I started 2 years ago. I was always trying new things, which sometimes included adding different frameworks or other external JS files.

Now I don't know which of the linked files/lines of JS are unused. Is there a tool which can detect these files and lines of code? It would save me a lot of time.

like image 367
Michael Schmidt Avatar asked Nov 01 '12 10:11

Michael Schmidt


People also ask

What is the best way to detect a cycle in a linked list?

A simple solution is to use hashing. The idea is to traverse the given list and insert each encountered node into a set. If the current node already presents in the set (i.e., it is seen before), that means a cycle is present in the list.

What is the two pointer approach to detect a loop in a linked list?

Detecting a loop Step 1: First, we will initialize two pointers, i.e., S as a slow pointer and F as a fast pointer. Initially, both the pointers point to the first node in the linked list. Step 2: Move the 'S' pointer one node at a time while move the 'F' pointer two nodes at a time.


2 Answers

This answer offers Google's Closure Compiler which, in the process of minifying and concatenating your JavaScript code, can remove "dead code".

Quoting from the documentation for the compilation levels:

Compilation with ADVANCED_OPTIMIZATIONS removes code that is provably unreachable. This is especially useful in combination with large libraries. If you use only a few functions from a large library file, the compiler can remove everything except those functions from its output.

Also see this answer which contains more information on Google's Closure Compiler.

like image 148
Whymarrh Avatar answered Oct 04 '22 04:10

Whymarrh


I had this need so I created a tool that detects unused JS on the browser side, not just from the sources, so it can also test third parties scripts.

It works by creating a local proxy on your computer that intercepts JavaScript requests and instruments these files on-the-fly. The tool is than able to detect which parts of the instrumented files have been used by the page, and which don't.

I made it open-source and you can find it here: https://github.com/gmetais/unusedjs.

like image 45
Gaël Métais Avatar answered Oct 04 '22 04:10

Gaël Métais