Eliminating Code Rot – JavaScript Edition


At AddThis, we’re always striving to deliver top-quality web tools. Our tools should not only look great and function across all platforms, but they should do so with as small an impact as possible on the load times of your website.

As a code base gets older, code rot is inevitable. When JS code runs in the global scope, code rot can persist even through advanced minification. A contrived example:

  <button onClick="myfunction()"/>
    function myfunction() {

If the button is removed, there is no way `myfunction` would ever get called, but your minifier cant _know_ that for sure. Alas, by cleaning old or unused features in our UI, we have introduced code rot into our application.

In an effort to combat this, AddThis will be deploying special instrumented code  to a small (1% or less) portion of users. After these users leave a page with AddThis tools on it (or after a sufficiently long time, in browsers which do not support the sendBeacon API), AddThis will send a small report to its servers which contains information about the lines of AddThis code that actually ran during the users time on the page.

After a couple of days, we will compile the data, and use it to optimize our code in two ways: First, we will be able to completely remove code which is completely unused. Second, we will move code which is only used by a small percentage of people out of the main javascript file, to improve performance in the general case.

We’ll publish the results of this project when we’re done, so keep an eye out!