npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unusedjs

v0.2.2

Published

Discover the percent of unused JS on a page with this simple browser proxy

Downloads

20

Readme

There are already several tools that tell you the percent of unused CSS on a webpage. But what about unused JS? Well this tool does it for JS.

It is a browser proxy written in NodeJS.

How does it work ?

  1. The proxy intercepts incoming javascript files.
  2. Each script is instrumented on the fly by a test coverage tool (Istanbul).
  3. When the script executes on the page, coverage metrics are collected in the background.

Be careful, the proxy is not working with HTTPS files.

JS files loaded over HTTPS are ignored. Proxies can't intercept SSL communication.

Installation

You need to open your console and write:

npm install unusedjs -g

Use

  1. Start the server by writing in your console: unused-js-proxy

  2. Configure your browser's proxy to localhost:3838. Only set the HTTP proxy, let the HTTPS (=SSL) proxy empty.

  3. Clear your browser cache <== IMPORTANT

  4. Open your browser's and wait until the page is fully loaded

  5. Open your browser's console and write _unusedjs.report()

Results

Results are displayed in the console:

screenshot

Why "(for the moment)"? Because the score might change if some more JS gets executed in the page.

Inspect what code is unused for one file (experimental)

_unusedjs.file(<file number>)

There are stille some bugs with very large files, especially when minified on one very long line. Best displayed on Chrome or Safari.

screenshot

Troubleshooting / FAQ

_unusedjs is not defined

That means no JS file was instrumented by the proxy. Make sure the page you are testing is not HTTPS. Make sure the page loads at least 1 script and it's not over HTTPS. Make sure the proxy is still running and is not displaying errors. Then make sure you configured correctly your browser's proxy.

The proxy fails with an error

I did not debug this error yet. Can you?

The console doesn't display the colors

Your browser may not be compatible with console.log styling.

The page loads slower

Yes. The JS files are instrumented by the proxy and this step is slow. And it's not parallelized. Don't forget to kill the tool when you're done, otherwise you might experience a sloooooow surfing session.

Inlined scripts are not analyzed

Sorry.

Does XX% of unused code mean I should remove it?

It's not so easy. It can be some code that's not executed at page load, triggered by a user action for example. If it's a library (such as jQuery), removing the unused parts is pretty hazardous.

My trouble / question is not listed here

Just open a GitHub issue :)

What's next with this tool?

For the moment it's just a quick proof of concept. Tell me if the tool is interesting, because here are some ideas for the future:

  • automatically make the measures on domContentLoaded, domContentLoadedEnd and domComplete (can help defer scripts after the critical path).
  • automatic launch in PhantomJS configured with the proxy.
  • a service worker that does this automatically (on localhost).

Author

Gaël Métais. I'm a webperf freelance. Follow me on Twitter @gaelmetais, I tweet mostly about Web Performances.

If you understand French, you can visit my website (will be soon in English too).