Your SlideShare is downloading. ×
0
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
JavaScript Variable Performance
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

JavaScript Variable Performance

7,877

Published on

My talk at the January 21, 2009 Mountain View JavaScript Meetup about the performance of JavaScript variables relative to their position in the scope chain.

My talk at the January 21, 2009 Mountain View JavaScript Meetup about the performance of JavaScript variables relative to their position in the scope chain.

Published in: Technology
4 Comments
18 Likes
Statistics
Notes
  • @Albin
    In this method:
    for (var i=0; i < items.length; i++) {
    JS has to determine the length of items each and every loop.

    Instead, you can declare an additional variable, len, and then JS needs only to do a simple comparison of the two variables each and every loop:
    for (var i=0, len=items.length; ++i < len;) {

    Also note the use of ++i in the comparison versus i++ as the third parameter of the for loop. This is documented as a faster incrementing method by Paul Irish: http://paulirish.com/i/d9f0.png

    Happy coding,
    Atg
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Variables
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Because property access in JavaScript is slow, and doing it each time through a loop is expensive. See my other presentation, Writing Efficient JavaScript.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • At slide #3: Why is items.length marked?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
7,877
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
189
Comments
4
Likes
18
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. JavaS cript Variable Performance Nicholas C . Zakas P rincipal Front E nd E ngineer, Yahoo!
  • 2. S cope C hains
  • 3. When a Function E xecutes • An execution context is created • The context's scope chain is initialized with the members of the function's [[S cope]] collection • An activation object is created containing all local variables • The activation object is pushed to the front of the context's scope chain
  • 4. E xecution C ontext Identifier R es olution • S tart at scope chain position 0 • If not found go to position 1 • And so on
  • 5. Identifier R es olution • Local variables = fast! • The further into the chain, the slower the resolution
  • 6. Identifier R es olution (R eads ) Firefox 3 Firefox 3.1 Beta 2 Google Chrome 1 Internet Explorer 7 Time Internet Explorer 8 Beta 2 Opera 9.62 Safari 3.1 (525.13) Safari 3.2 (525.27.1) WebKit Nightly (528+) 1 2 3 4 5 6 Identifier Depth
  • 7. Identifier R es olution (Writes ) Firefox 3 Firefox 3.1 Beta 2 Google Chrome 1 Internet Explorer 7 Time Internet Explorer 8 Beta 2 Opera 9.62 Safari 3.1 (525.13) Safari 3.2 (525.27.1) WebKit Nightly (528+) 1 2 3 4 5 6 Identifier Depth
  • 8. R ecommendations • S tore local references to out-of-scope variables – E specially global variables • Avoid the with statement – Adds another object to the scope chain, so local function variables are now one step away
  • 9. E tcetera • M y blog: www.nczonline.net • M y email: nzakas@ yahoo-inc.com • Twitter: @ slicknet

×