Friday, July 1, 2011

Writing useful JavaScript applications in less than half the size of jQuery

Not too long ago, I tried to bring attention to how little of the jQuery library many developers actually use and argue that frontend developers should consider what sort of improvements their users would see if they could compile their code with the Advanced mode of the Closure Compiler. Today I would like to further that argument by taking a look at TargetAlert, my browser extension that I re-released this week for Chrome.

TargetAlert is built using Closure and plovr using the template I created for developing a Chrome extension. The packaged version of the extension includes three JavaScript files:

Name Size (bytes) Description
targetalert.js 19475 content script that runs on every page
options.js 19569 logic for the TargetAlert options page
targetalert.js 3590 background page that channels information from the options to the content script
Total 42634  

By comparison, the minified version of jQuery 1.6 is 91342 bytes, which is more than twice the size of the code for TargetAlert. (The gzipped sizes are 14488 vs. 31953, so the relative sizes are the same, even when gzipped.)

And to put things in perspective, here is the set of goog.require() statements that appear in TargetAlert code, which reflects the extent of its dependencies:
I include this to demonstrate that there was no effort to re-implement parts of the Closure Library in order to save bytes. On the contrary, one of the primary reasons to use Closure is that you can write code in a natural, more readable way (which may be slightly more verbose), and make the Compiler responsible for minification. Although competitions like JS1K are fun and I'm amazed to see how small JS can get when it is hand-optimized, even the first winner of JS1K, Marijn Haverbeke, admits, "In terms of productivity, this is an awful way of coding."

When deploying a packaged browser extension, there are no caching benefits to consider: if your extension includes a copy of jQuery, then it adds an extra 30K to the user's download, even when gzipped. To avoid this, your extension could reference (or equivalent) from its code, but then it may not work when offline. Bear in mind that in some parts of the world (including the US! think about data plans for tablets), users have quotas for how much data they download, so you're helping them save a little money if you can package your resources more efficiently.

Further, if your browser extension has a content script that runs on every page, keeping your JS small reduces the amount of code that will be executed on every page load, minimizing the impact of your extension on the user's browsing experience. As users may have many extensions installed, if everyone starts including an extra 30K of JS, then this additional tax can really start to add up! Maybe it's time you gave Closure a good look, if you haven't already.

Want to learn more about Closure? Pick up a copy of my new book, Closure: The Definitive Guide (O'Reilly), and learn how to build sophisticated web applications like Gmail and Google Maps!


  1. Man, I really dig the intent of your post, but Closure is just so painful to code in. The beauty of jQuery is in its simplicity and eloquence, without sacrificing power. Closure feels so much like Java, YUI and other unnecessarily verbose APIs.

    Particularly since this is an extension -- making caching irrelevant -- did you ever consider using jQuery, but compiling the entire app w/ Closure Compiler to reduce the amount of jQuery that gets included?

  2. Devil's advocate: I wonder what percentage of machines have jQuery in their browser cache. JQuery *is* needlessly huge, and lots of it could be compiled away, but then I'd have to redownload all the common internals that every page uses.

  3. @Jason: I'm guessing the cache is irrelevant for most apps, unless they all share the exact same version, from the *same URL*. Even then you have parse time to consider, which on mobile can add up quickly (easily a few hundred ms in this case).

  4. I'm wondering why Chrome or other browsers don't support jQuery so that whenever there is a script loading jQuery or jQuery UI (and it's css templates) from:

    It will just use the browser's local files.

    This way, all this Closure discussions can be avoided.

  5. Akishore: You cannot just "compile jQuery in Advanced mode" as jQuery is implemented today. Two things that I am considering are (1) creating an alternative implementation of jQuery that is Closure Compiler friendly, or (2) creating an option for Coffeescript so it generates Closure-style JavaScript that can be compiled in Advanced mode. I'm much more interested in the latter, but would consider taking on the former as contracting work.

  6. Felix: The focus should not be on jQuery, but on how browsers manage resources, in general. For the Google-hosted JS libraries, they should be served with the correct cache headers so that things basically work as you describe (when the browser sees the URL, it retrieves the file from the cache and never goes to the server).

    However, there are two problems with this solution. The first is that some sites do not want to depend on Google as a CDN and would rather use their own. Unfortunately, I don't believe there's a way in HTML/JS to say, "Here is a list of URLs to a specific version jQuery: just load the first one that you have in your cache. If none are in cache, then just fetch the first one in the list." If such an API existed, you could easily use Google as your primary CDN but still use your own CDN as a backup.

    The second problem is that you are ignoring parse/execution time which increases the size of the library. Even if you can always pull jQuery out of the cache when you need it, it still has to be parsed (though the parse tree could also be cached) and executed for each page that uses it. Therefore, as you increase the size of the library, you take more of the user's time.

  7. Jason/joel: Here is some good evidence that many users should have some version of jQuery (that is hosted by Google) cached in their browser:

    However, as the author notes, sites do use different versions of jQuery, which means they fetch different URLs, which eliminates any benefit from caching.

    As I noted in my response to Felix, it's also important to consider the additional execution time that results from loading a larger JS library, even if it is cached.

  8. Norton by Symantec Corporation comes with a promise to protect both your online and offline world. Norton antivirus program protects users’ system from visiting the websites which are harmful and suspicious; moreover, it restricts the system from downloading malicious files.


  9. You have written such a interesting is worth of reading.keep updating and sharing with us

    Digit me
    Zen Watson

  10. Usually, I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man, Keep it up. We provide the highest quality in mold remediation services, asbestos removal & water dry out in Maricopa County including Phoenix, Mesa, Chandler, Glendale, Scottsdale, and Gilbert! visit us for more information Best Blown In Insulation Attic Insulation | Best Drain Cleaning Services Arizona | Best Reverse Osmosis Services Arizona | Best Plumbing Services Arizona | Best Plumbing Inspection Services Arizona

  11. You are really a talented person such a informative and helpful content you provided with us.
    Your information on Java script really appreciated my all problems are solved...Thanks so much sharing a great setup.. keep shine and Rise...

  12. This comment has been removed by the author.

  13. That's amazing and interesting to write "JavaScript applications in less than half the size of jQuery".

  14. The post is good of how to write java script application, firstly we can start & check if it works good, then we accordingly start & the procedure is mentioned easily.

  15. Designed to help you handle tasks with ease, Microsoft 365 will bring out the best in you. Before you get started, explore how Microsoft 365 can enhance your business and work life.
    Microsoft365.con/setup | Microsoft365.con/setup | Microsoft365.con/setup | Microsoft365.con/setup | Microsoft365.con/setup

  16. Using your Microsoft Account associated with office to sign in will take you to the Dashboard for Office installation, If you are new to Office, you can create a New account and enter Product key to redeem and Install Office.

  17. Exhibitors can interact face-to-face with leads that wander past their booths and scan their badges to collect their information but lead gen happens very differently in a virtual environment. motivational speaker bio example and sample bio for speaker

  18. Visit our website and Troubleshoot the most commonly occuring errors of Microsoft office by the easiest steps. Install, and activate your Office setup on your computer and other devices.

  19. Are you getting errors when you try to activate your Microsoft office activation subscription? Then read on to get step by step guide to solve Microsoft office activation errors. Install, and activate your Office setup on your computer and other devices.

  20. Microsoft 365 provides more security than a basic Office 365 subscription, with features such as Enhanced Security Features, wireless cleaning, and app safety and security that prevents duplication.. Instead of using Microsoft Word and Excel, Microsoft 365 offers a range of useful features.

  21. Home and Student 2019 is designed for students and families who choose to use classic Office software such as Excel, Word, and PowerPoint on Windows and Mac. It’s a one-time purchase that can be used at home or in the classroom on one PC or Mac. home and student 2019 home and business 2019 home and student 2019 home and business 2019

  22. Bitdefender is the tool to provide you best security from multiple cyberattacks. It offers protection for multiple devices under one subscription, which can be managed by a single device.
    Bitdefender Central

    turbotax login

  23. Welcome to the HP site. Download the software SW from hp setup and set up your printer. Then, Get started to installed hp setup and get able to connect the printer to the network through a USB cable or use a wireless HP printer.

  24. is an online platform that is a creation of Fox Corporation and stream the latest content and videos of your interest. In comparison to Netflix, the platform is free to access and you can watch as many as movies you want for free with the help of

  25. Microsoft 365 plans for personal and home provides robust Office desktop apps including Word, PowerPoint, Excel, Outlook, and OneNote. | allows you to download and activate the Microsoft Office setup. Go through the webpage,e, click Set Up and start the Canon ij printer setup process.

  26. Cricut provides some of its products for different projects where Cricut design space, Cricut Joy, Cricut Explore, and some others are included , ij.start canon , ij.start canon , , , ,ij.start canon .

  27. A Quick Guide to Canon Printer Setup
    Make sure your printer hardware is configured.
    On your PC browser, go to
    Click Set Up.
    Using your Canon IJ printer model, the download printer software.
    Double click to install it.
    Connect a network to your printer and PC.
    Finish the Canon IJ printer setup.
    LaserJet Printer Setup
    Configure your HP Printer hardware.
    Visit from the browser.
    Using your LaserJet model name, download software.
    Double click to start the installation.
    Connect the printer via wireless network or USB.
    Add the LaserJet printer to your system devices.
    Finish the HP LaserJet setup.

  28. allows you to download and activate the Microsoft Office setup. Microsoft 365 plans for personal and home provides robust Office desktop apps including Word, PowerPoint, Excel, Outlook, and OneNote.Microsoft 365 subscribers can always install the latest version of MS Office and get frequent software updates. If you have a Microsoft account, sign in and enter the product key at to activate your product. Install and set up your Office 365 apps with the given instructions.

  29. Canon Pixma TS3322 comes with a replaceable 2-Cartridge FINE Hybrid Ink System, including an uplifted version with Wireless Connect in use, a Paper Tray on the rear side that enables reloading plain and photo paper effortlessly fast.

  30. Canon printer drivers and software are essential to install and set up your Canon device. Visit bitdefender and use multiple activities after the Sign in to this page. To access the Cox Webmail Login page, you'll need an internet service and a Device.
    cox webmail login |
    cox mail login | |

  31. Camps Intuit is the portal that provides you the QuickBooks business accounting and management software available. You can download the software with , , skybet login , , trend micro activation .

  32. The perfect way to get started with is gathering enough information and purchasing depending on your requirement. You can then easily download and install the software package by visiting .

  33. The process to set up PIXMA ts3122 starts from and download the latest full driver & software package for PIXMA TS3122. You can learn to set up this multifunction printer model. Instructions on this page include everything from printer unpacking, installation, configuration, WiFi network establishment to complete canon printer drivers setup. Let’s get started.

  34. ij.start canon is the manufacturer site to download Canon printer drivers. Install and set up Canon Printer from | | cannon/ijsetup | Cox High-Speed Internet provides its users with COX Communications..Cox webmail Login

  35. The Webroot program is highly rated software to protect your devices & data available for download at| ij.start.cannon/ts3322 |

  36. Enter Trend micro activation code on | www.trendmicro/activate to download and activate Trend Micro. To activate Trend micro, make sure you already have an activation code that you’ll probably enter on the site.

  37. While this may not be an ideal phone, it may have some limitations in terms of screen quality and camera quality, but it offers better specs than similarly priced phones like the Xiaomi Mi 10T Lit.

  38. To access the Belkin router login interface, you will need the Belkin router, access to the network, and a web browser. You can access the network through a LAN cable or with your Wi-Fi connection. You will also have a connecting device such as a computer, phone, or tablet with a web browser on it to access the login page.

  39. To rank your website top on the search engine, hire an SEO company in Gurgaon and get the best services at an affordable price. Being a reliable SEO agency in Gurugram we understand the need of our customers.
    SEO company in Gurgaon

  40. AOL MAIL LOGIN is all about convenience and efficiency. As such, creating and accessing your Email Account is extremely simple and quick. For this, you must go to its official website i.e. “”. In order to create/ access your account smoothly without any hassle, you must follow the instructions given below.
    For More Information Visit Our Site:

  41. Promotion is the best way to get more profit. If you want to earn more profit hire a Corporate Video Production Company in Gurgaon, to get innovative Instagram, facebook videos and social marketing videos.
    Video Production Company in Gurgaon

  42. To rank your website top on the search engine, hire an SEO company in Gurgaon and get the best services at an affordable price. Being a reliable SEO company in gurugram we understand the need of our customers.
    SEO company in gurugram

  43. This is definitely one of the best Articles till now. No wonder why people are liking your blog but now I can say This is the best article I have ever read. Thank you so much for sharing this article with us. 쇼미더벳 토토사이트, 쇼미더벳, 쇼미더벳 카지노사이트, 쇼미더벳 파워볼사이트, 쇼미더벳 회원가입코드, 쇼미더벳 슬롯머신