Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jQuery slow down the rendering of webpages by 100ms or more?

I am trying to improve the frontend page load speed for users, and I am finding that jQuery is slowing the DomContentLoaded event down by more than 100ms.

I am benchmarking on Windows 7 with Chrome 17 using a computer with an i5 2.5Ghz, SSD drive, and 8GB of RAM. The test is run on my local computer. I'm concerned that the slow speed I see on my machine will be even slower on older computers and browsers.

Is this just the standard penalty for using jQuery, or is there a way to speed up the performance that I am missing?

Here is the code that I am using:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
            console.time("DOMContentLoaded");
        </script>
    </head>
    <body>
        <h1>Hello World</h1>

        <script type="text/javascript" src="/js/jquery-1.7.1.min.js"></script>

        <script type="text/javascript">
            document.addEventListener( "DOMContentLoaded", ready, false );

            function ready() {
                console.timeEnd("DOMContentLoaded");
            }
        </script>
    </body>
</html>

On the console, the time that I see is roughly ~100ms.

When I remove the line that loads jQuery, the time is roughly ~1ms.

I also tried the code above using the Google CDN:

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

The result is largely the same.

Is there always a 100ms penalty for using jQuery? Is there something that I am missing? Thanks!

like image 765
Chris Avatar asked Mar 10 '12 21:03

Chris


People also ask

Does jQuery slow down a website?

jQuery is an easy way to get a result, but the way it loads is bad. The load process blocks everything else and makes your site feel slow. jQuery is also far from fast loading. It is heavy and slows page loading down a lot.

Does jQuery affect performance?

For small websites and simple web apps, the advantages of jQuery will often outweigh the minimal performance impact. For larger websites and web apps with thousands of lines of code, or those processing large amounts of data using JavaScript then performance is absolutely something to consider.

Does JavaScript make your website slow?

Poorly written JavaScript code can slow your website, negatively affecting load times and rendering speed.

Is jQuery fast?

jQuery in terms of speed is quite fast for modern browsers on modern computers. So is pure JavaScript. Both run drastically slower on older browsers and machines. Pure Javascript to access the DOM can be faster as you can cut the overhead that jQuery has on this.


2 Answers

You are going to see a small hit whenever loading a large JS library like jQuery. Personally, I'd argue that 100ms isn't much, you have to keep in mind ALL of the factors leading to this:

  1. You are timing the load, which takes time.
  2. Your network connection plays a factor (there's parse document, make request, wait for response). Latency of your connection must be factored in (use http://www.speedtest.net to test your latency, and subtract that from the 100ms+ to get a better idea of the actual hit).
  3. Proper cache control on your web server can all but eliminate #2, configure that server so the file only expires once every month or so. This header will then be passed with the file so the user's browser only loads it once a month. You will still incur the small request on the first page load, but at least then subsequent page requests will pull it from the local cache reducing this time.
  4. There's parse an execution time; The ENTIRE jQuery file needs to be parsed, and executed. Minifying only reduces the size, and therefore the bandwidth required to transfer it (which equates to time based on connection speed)... minifying does very little to reduce parse time (modern JS engines like V8 fly over comments and whitespace as if they weren't even there).

Keep in mind, that most pages won't have NEARLY as much custom JS (based on jQuery) as the actual library itself, so even an intensively interactive page will only see a few [dozen] milliseconds of additional overhead over what the library itself has already imposed.

As for people with old machines/browsers; chances are they're experiencing poor performance on every page they visit. The web is a dynamic place, you have to keep up if you want to have a good experience. There's only so much you can do for luddites.

like image 97
AC2MO Avatar answered Oct 22 '22 23:10

AC2MO


jQuery is a 92K file. You have to load it and parse it.

I have hosted your example at studio831.com/jquery_test/index.html, cdn.html, and dom_ready.html.

You can look at the times each take to download. You can also use Chrome's developer tools to see the breakdown how long downloads takes.

like image 38
Steve Wilhelm Avatar answered Oct 22 '22 21:10

Steve Wilhelm