Speeding Up Tesla.com - Part 1: Images and JS Minification
Tags: webperf, javascript, css
Published: 2020-06-01 11:40:06

I like Tesla. I think they are a great company, working towards a better future. That's why I was on their website recently, on a pretty average connection, and I noticed that it is loading very slowly. So of course, me being me, I opened Firefox dev tools and started digging around.

**TLDR**: At the end of the day I managed to speed up homepage time to interactive from over 17 seconds to around 4 seconds by removing unnecessary things and optimizing assets delivery.

This two-part article series is meant to show a real-world use case of how you can:

1. Notice problems - Run webpagetest / Lighthouse on your website
2. Identify rooms for improvement - Look for heavy resources, number of requests and pay special attention to CSS because it's blocking
3. Fix
    1. Remove what's not needed
    2. Split into smaller chunks what is needed
    3. Lazy load what is not critical
    4. Load on demand what is not used by at least half of the users

Those are the easiest techniques, that don't involve rewriting the code or changing any functionality of the page, which is what this audit/article is supposed to demonstrate.

## Problems

After running webpagetest and Lighthouse on the site, I identified the following problems: 

1. The main photo is in PNG format (7.7 MB)
2. Non minified Boomerang.js library ([http://www.boomerangjs.org/](http://www.boomerangjs.org/))
3. Some fonts are inlined inside CSS but never used
4. Some fonts are inlined, but really should not be
5. Most inlined assets are duplicated (huge savings can be achieved there)
6. Some assets for all 44 languages are included in the main CSS file, even though a user can use only one at a time
7. A lot of media queries, which at this size should be separated into multiple CSS files
8. Main JS file is heavy and it looks like it could be divided into smaller parts and lazy loaded

We won't tackle some of those problems, because the nature of JavaScript is too complicated and I don't know this website well enough to decide which script can or cannot be loaded on demand. This is a task for Tesla developers, I can only help their decisions with my advice. 

## Solutions

I did the following to speed up the site: 

1. Compress image and use JPEG format - saved 6.7 MB + improved perceived performance by using progressive JPEG
2. Remove unused font - made the page lighter by 150 KB, and because it was inside CSS, it is no longer blocking rendering
3. Minify the boomerang.js library - 120 KB → 40 KB

## Performance before 
I made some baseline measurements using [webpagetest.org](http://webpagetest.org) ([see report](https://webpagetest.org/result/200514_GH_22675a135a6689badd0ba1bad7cf2744/)) and Lighthouse:

![before webpagetest](https://uploads.prod01.oregon.platform-os.com/instances/1374/assets/images/blog/tesla-part-1/before-webpagetest.png)

In webpagetest, the most significant thing is Largest Contentful Paint. This means that this 7 MB hero image was loaded after 16 seconds.

![Lighthouse before](https://uploads.prod01.oregon.platform-os.com/instances/1374/assets/images/blog/tesla-part-1/before-lighthouse.png)

In the Lighthouse test, I like to pay special attention to Time to interactive, and First CPU Idle - this indicates when the user can start using your page (meaning, JavaScript is not blocking the main thread anymore). 

## Head image solution

Let's address the first issue first because it is the lowest hanging fruit of the bunch and does not require **any** technical knowledge. As I wrote in my article [Optimizing Images for the Web](https://dev.to/platformos/optimizing-images-for-the-web-18gc)  you can basically tell which format should be used for an image by the contents of it. In this case, it should be JPEG and I'm going to use MacOS Preview to save it as a JPEG and then compress it using ImageOptim.

![Preview saving](https://uploads.prod01.oregon.platform-os.com/instances/1374/assets/images/blog/tesla-part-1/preview-saving.jpg)

![Imageoptim](https://uploads.prod01.oregon.platform-os.com/instances/1374/assets/images/blog/tesla-part-1/imageoptim.png)

To make sure that I'm not going to degrade the quality of the image (it is a product page, so it has to look good) I used quality >90, which produces minimal artifacts. Results are pretty good, saving over 7 MB on that one image. Additionally, JPEG is progressive, so it will load in a much more pleasant way than PNG.

## Unminified boomerang.js library

This is also a simple task to do, just take the contents of the file and paste it into a JavaScript minifier. If you use the DuckDuckGo search engine, just type: "js minifier online" and it will pop right up. Minification made the file smaller by 80 KB, 120 KB → 40 KB. Not bad for 20 seconds of work. 

![Minification](https://uploads.prod01.oregon.platform-os.com/instances/1374/assets/images/blog/tesla-part-1/minification.png)

### Unused font

I noticed that the `book-override.font.css` file had inlined some Gotham fonts, but never used them. I removed the reference to them from the HTML file altogether.

## Result

As a result of those operations, the site got lighter by around 7 MB without any advanced techniques.

Let's see what Lighthouse says about it (after it has been uploaded to [platformOS](https://www.platformos.com) as hosting):

![Lighthouse after](https://uploads.prod01.oregon.platform-os.com/instances/1374/assets/images/blog/tesla-part-1/after-lighthouse.png)

A little bit better. Let's hope the next steps will improve it further.

In part two of this series, we will take a look at the huge (2.5 MB!) CSS file and cut it down to 365 KB.

## Update

Since I started writing this article, [Tesla.com](http://tesla.com) did exactly what I described here in regards to images, and even used more aggressive compression, so their image is 370 KB, which means there was no need to have it in such a good quality after all. :)

## Source code

You can see the results and source code here:

Part 1: [https://github.com/pavelloz/tesla.com](https://github.com/pavelloz/tesla.com) - https://tesla.prod01.oregon.platform-os.com/part-1

Part 2: [https://github.com/pavelloz/tesla.com/tree/part-2](https://github.com/pavelloz/tesla.com/tree/part-2) - [https://tesla.prod01.oregon.platform-os.com/part-2/](https://tesla.prod01.oregon.platform-os.com/part-2/) and [https://tesla.prod01.oregon.platform-os.com/part-2-de/](https://tesla.prod01.oregon.platform-os.com/part-2-de/)

## Read more

If you are interested in more performance oriented content, follow me and I promise to deliver original, or at least effective methods of improving your website. 

{% link https://dev.to/platformos/using-webp-in-your-existing-webpage-809 %}

{% link https://dev.to/platformos/optimizing-images-for-the-web-18gc %}

{% link https://dev.to/platformos/3-tips-on-preserving-website-speed-5g0c %}