English

Optimise your Javascript — debunk

Today netmagazine.com published an article highlighting a bunch of javascript performance tips. While focusing on javascript performance is indeed laudable, and I don’t want to be too critical it is also important that we correct malinformation. Sadly this article would happen to lead people slightly off course in their quest to write performant javascript.

I’ve prepared jsperf tests of most the example code from the article so you can judge by yourself:

As you can see for yourself some tips are solid, while others are just not true, but overall theres just one tip I would truly recommend to you and that is that DOM operations are expensive.  So dont focus on minor optimiziations, focus on what matters;

  • Fewer DOM operations
  • Smaller markup
  • Simpler CSS
  • Minimize your JS
  • Browser caching
  • JS caching / memoizing.
  • Perceived speed (through optimistic operations for example — rendering result before server response).

That being said, some of these tips are good to maintain performance in older browsers. String building through arrays perform far better in old browsers, while its quite well optimized in your normal every day web developers chosen browser. You also need to think about who you are targeting; If you need to cater for a bunch of IE8 users you need a different optimization focus than for Chrome Canary users.

About Raymond Julin

Lead developer at Keyteq Labs, a product business in Bergen, Norway with a reputation for modern user friendly solutions.

2 Comments

  1. Some of the perf tests you did are “biased”. Browsers are smart to cache some of the results and avoid doing unnecessary work. I wrote about some common benchmarking pitfalls some time ago.

    Even if some of the things suggested are indeed faster it is still premature optimization unless you have a good reason to do it and only after identifying the real bottleneck.

    Prototype/scope lookup and function calls is so fast in the latest engines that it shouldn’t matter on 99% of the cases (same goes for most of the other things listed in the article). Write clean code, worry about perf later if needed.

    Cheers.

    • Raymond Julin

      I was a bit unclear in my writing as I hacked it up quickly before going to bed. I tried to make the same point as you; All of these tips aren’t really worth optimizing for until you’re at the point where you squeeze the last drops out.

      I believe my shortlist with DOM manipulation, markup, css, caching, etc are things you need to actually care for. A highly modern and popular device like the iPad has its performance issues you can run into easily for single page applications and other js heavy pages. Thanks for the feedback!

      Cheers.

Leave a Reply