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.
5. 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
6. 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
7. Identifier R es olution
• Local variables = fast!
• The further into the chain, the slower the
resolution
8. 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
9. 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
10. 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
11.
12.
13. E tcetera
• M y blog: www.nczonline.net
• M y email: nzakas@ yahoo-inc.com
• Twitter: @ slicknet