Is there a tool to remove unused methods in javascript?

Solution 1:

No. Because you can "use" methods in insanely dynamic ways like this.

obj[prompt("Gimme a method name.")]();

Solution 2:

Check out JSCoverage . Generates code coverage statistics that show which lines of a program have been executed (and which have been missed).

Solution 3:

I'd like to remove all the unused methods to get size down to a more reasonable level.

There are a couple of tools available:

npm install -g fixmyjs
fixmyjs <filename or folder>

A configurable module that uses JSHint (Github, docs) to flag functions that are unused and perform clean up as well.

I'm not sure that it removes undefined functions as opposed to flagging them. though it is a great tool for cleanup, it appears to lack compatibility with later versions of ECMAScript (more info below).

There is also the Google Closure Compiler which claims to remove dead JS but this is more of a build tool.

Updated

If you are using something like Babel, consider adding ESLint to your text editor, which can trigger a warning on unused methods and even variables and has a --fix CLI option for autofixing some errors and style issues.

I like ESLint because it contains multiple plugins for alternate libs (like React warnings if you're missing a prop), allowing you to catch bugs in advance. They have a solid ecosystem.

As an example: on my NodeJS projects, the config I use is based off of the Airbnb Style Guide.