• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
JavaScript Variable Performance
 

JavaScript Variable Performance

on

  • 11,262 views

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.

Statistics

Views

Total Views
11,262
Views on SlideShare
11,201
Embed Views
61

Actions

Likes
18
Downloads
187
Comments
4

5 Embeds 61

http://www.linkedin.com 28
http://www.slideshare.net 25
https://www.linkedin.com 5
http://localhost 2
http://translate.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

14 of 4 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • @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
    Are you sure you want to
    Your message goes here
    Processing…
  • Variables
    Are you sure you want to
    Your message goes here
    Processing…
  • Because property access in JavaScript is slow, and doing it each time through a loop is expensive. See my other presentation, Writing Efficient JavaScript.
    Are you sure you want to
    Your message goes here
    Processing…
  • At slide #3: Why is items.length marked?
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    JavaScript Variable Performance JavaScript Variable Performance Presentation Transcript

    • JavaS cript Variable Performance Nicholas C . Zakas P rincipal Front E nd E ngineer, Yahoo!
    • S cope C hains
    • 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
    • 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
    • Identifier R es olution • Local variables = fast! • The further into the chain, the slower the resolution
    • 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
    • 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
    • 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
    • E tcetera • M y blog: www.nczonline.net • M y email: nzakas@ yahoo-inc.com • Twitter: @ slicknet