6. Lexical scope used to create scope chain
Scope chain used as map to resolve symbolic reference
Local vars resolved from most specific to least
Worst case is expando variable creation
DOM is at the end of the scope chain
9. function BuildUI()
{
var baseElement = document.getElementById(‘target’);
baseElement.innerHTML = ‘’; // Clear out the previous
baseElement.innerHTML += BuildTitle();
baseElement.innerHTML += BuildBody();
baseElement.innerHTML += BuildFooter();
}
10. function BuildUI()
{
var elementText = BuildTitle() + BuildBody() + BuildFooter();
document.getElementById(‘target’).innerHTML = elementText;
}
11. function CalculateSum()
{
var lSide = document.body.all.lSide.value;
var rSide = document.body.all.rSide.value;
document.body.all.result.value = lSide + rSide;
}
12. function CalculateSum()
{
var elemCollection = document.body.all; // Cache this
var lSide = elemCollection.lSide.value;
var rSide = elemCollection.rSide.value;
elemCollection.result.value = lSide + rSide;
}
13. function IterateWorkOverCollection()
{
var length = myCollection.getItemCount();
for(var index = 0; index<length; index++)
{
Work(myCollection[index]);
}
}
14. function IterateWorkOverCollection()
{
var funcWork = Work;
var length = myCollection.getItemCount();
for(var index = 0; index<length; index++)
{
funcWork(myCollection[index]);
}
}
15. function IterateWorkOverCollection()
{
var parentElement = document.getElementById(‘target’);
var length = myCollection.getItemCount();
for(var index = 0; index<length; index++)
{
parentElement.appendChild(myCollection[iterate]);
}
}
16. function IterateWorkOverCollection()
{
var funcAppendChild = document.getElementById(‘target’).appendChild;
var length = myCollection.getItemCount();
for(var index = 0; index<length; index++)
{
funcAppendChild(myCollection[index]);
}
}
17.
18. var smallerStrings = new Array();
var myRatherLargeString = quot;quot;;
var length = smallerStrings.length;
for(var index = 0; index<length; index++)
{
myRatherLargeString += smallerStrings[index];
}
19. var smallerStrings = new Array();
var myRatherLargeString = quot;quot;;
smallerStrings.push(quot;<div id=quot;quot;);
smallerStrings.push(quot;sampleIDquot;);
markupBuilder.push(quot;quot;>quot;);
smallerStrings.push(quot;sampleTextquot;);
smallerStrings.push(quot;</div>quot;);
…
var myRatherLargeString = smallerStrings.join(‘’);
20. • Perform string operations using local vars
• Cache strings from IE objects
• Use Array.join for concatenation
22. • Problems
Script injection increases security risk
•
Setup and Parsing takes time
•
Error conditions sometimes overlooked
•
• Solution
Anticipate and scope code for specific problems
•
Use data to drive code
•
23. • Javascript - SWITCH becomes costly for large sets
Problem: Switch processing time grows linearly
•
Consider: Hash Table wrapped in Try, Catch
•
Why: Javascript does not optimize Switch
•
• Javascript - WITH
Problems: Increases symbol look up time everywhere
•
Consider: Use manual iterators
•
Why: Adds entry to the scope chain
•
24. • Property Accessor Functions (get/set)
Problems: Increases cycles to access values
•
Consider: Directly modifying variables
•
Why: Inserts an additional symbol lookup
•
25.
26. Instantiating DOM functions
Problems: Costly (in CPU cycles)
•
Consider: Be Informed/Intentional
•
Why: Platform Genericness
•
Accessing / Traversing DOM properties
Problems: Each access is costly
•
Consider: Caching values/function pointers
•
Why: Platform Genericness
•
39. Goal: Improve dev visibility into web app behavior
Cross-browser, no client-side software changes
Fine-grained, code-level monitoring
Flexible instrumentation: performance, state, etc.
Approach: “On-the-fly” JS rewriting adds instrumentation only when/where it is needed
Status: Tech preview release coming soon
Demo’ing now at Hands-on-Lab
http://research.microsoft.com/projects/ajaxview/
Future Directions:
Integration into IE developer toolbar
Integration into server-side tools to let dev see real-user experience
40.
41. Identify the type of performance problem
Network / Bandwidth – using Fiddler
Javascript – Using AJAX View
Aggressive DOM access – Using AJAX View
Reduce, Simplify, Re-factor
Reduce the bytes sent between the client/server
Simplify your code to avoid costly Javascript constructs
Cache DOM properties and function pointers