Optimize only when you have a reason to.

Measuring Javascript Parse and Load

While studies like this are fascinating to observe and to learn from, be very careful — unless you’re doing something like distributing JS libraries, these optimizations probably do not apply to you.

Keep in mind that the parse-and-load test runs 1000 iterations; in the first table, the actual average parse time is really only 0.125ms at worst and 0.003ms at best. Compare that to the average HTTP request — you might see sub-30ms responses just for a HTTP 304 on a good day, but 1000ms+ responses are not unheard of on unprimed caches (even more with DNS lookups). To put this into perspective, a snappy HTTP request at 30ms is more than 200 times slower than the slowest parse time of 0.125ms.

In other words, the net improvement you might gain from such parse-and-load optimizations is insignificant compared to the improvement from removing just one HTTP request.

So before you go adding more lines of code to accomplish yet another new trick, profile your code to see where the actual bottlenecks are. Otherwise, this smells like a lot like premature optimization.

Update: I’ve wrongly interpreted the results table and Carlos has corrected me. This means that the cost of script parsing and loading is significant, and optimizing this should help reduce CPU load which is hard to benchmark in real world conditions but could certainly be perceptible to users.

3 Responses To This Post:

  1. Carlos Bueno says:

    Hey — I feel I should clear something up. The tests contain 1,000 iterations, but each data point in the chart is the *average*, not the sum.

    It really does take tens to hundreds of milliseconds to parse and load a typical Javascript library on a typical browser as of 2010. Parse time is on the same order of magnitude as network time, and the purpose of this test was to show that it is something you need to pay attention to. You can download the benchmark here and try it out:
    http://github.com/aristus/parse-n-load

  2. Marvin says:

    Carlos, thanks for taking your time to correct me!

    I certainly won’t dispute any of your findings and assertions, but I do still maintain that script parsing and loading probably isn’t the slowest part of most websites; there are often bigger gains to be found by cutting down requests, optimizing caches etc.

    My intention here was really just to stress the importance (to not much of an audience, really :) ) of profiling before optimizing blindly.

  3. amd nvidia benchmark…

    [...]Marvin Tam | Optimize only when you have a reason to.[...]…

Leave a Reply