Good talk on JavaScript performance + some notes

28 בספטמבר 2010

2 תגובות



Smashing magazine posted a Seven Must-See Videos and Presentations for Web App Developers
finally had the chance to start watching these videos(on the bus :))
 
The first one deals with Speeding up your JavaScript by Nicholas C. Zakas
Very interesting talk:




 


While watching this remember that you should take the problematic places that need to perform better and make the changes there(as Nicholas also stated at the beginning of the talk and also in this post following the talk).
I think that performance should also guide us while writing the code,
BUT in most cases – the alternative that uses code that is a bit slower(say using jQuery that makes using closures very easy) – is more readable and maintainable and should be used.


 


here are some of my notes(for me.. you shuold just watch it)
* mind your scope, minimizing the scope chain is better(don't use with, try/catch)
* try to use less closures
* property accessing is costly(store it at a var in the function)


Loops:
* don't use for-in for itertating arrays
* shorten loops and the amount of work in each step
for(var i=0, len=values.length ;i<len;i++)
{
  process(values[i]);
}


//flip loops (50% less time)
var len=values.length
for(var i=len;i–;)
{
  process(values[i]);
}


$.each(function… – this creates a closure for each iteration – use regular loop if you can


Dom
* HTMLCollections – BAD – and are evaluated always(sort of live objects)
  if you need to access it frequently – copy it to an array.
* eliminate Reflow(when adding items to a dom list for example)
* use DocumentFragment as a container document.createDocumentFragment- and then add this to the dom.
* do dom manipulations outside of the dom
* don't touch the style a lot – define a class instead that contains all of these if possible


 

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

2 תגובות

  1. Rotem Bloom15 באוקטובר 2010 ב 17:47

    תודה על השיתוף תמיד כיף לקבל טיפים על ביצועים.
    תמשיך לכתוב יותר פוסטים אההההה

    רותם

    הגב
  2. Avi Pinto15 באוקטובר 2010 ב 19:58

    בכיף 🙂
    כן.. אני "קצת" מזייף עם עניין הכתיבה בשנה האחרונה..

    הגב