SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 240 | Next

Richard Wagner

"Professional iPhone and iPod touch Programming: Building Applications for Mobile Safari"

write(s);
Using the Window Object
The window object is faster to use because Mobile Safari does not have to navigate the DOM to respond
to your call. The following window reference is more efficient than the top three:
// Inefficient
var h=document.location.href;
var h=document.URL;
var h=location.href;
// More efficient
var h=window.location.href
Local and Global Variables
One of the most important practices JavaScript coders should implement in their code is to use local
variables and avoid global variables. When Mobile Safari processes a script, local variables are always
looked for first in the local scope. If it can ??™ t find a match, then it moves up the next level, then next, until
it hits the global scope. So global variables are the slowest in a lookup. For example, defining variable a
at the global level in the following code is much more expensive than defining it as a local variable
inside of the for routine:
// Inefficient
var a=1;
function myFunction(){
for(var i=0;i < 10;i++) {
var t = a+i;
// do something with t
}
}
//More efficient
function myFunction(){
for(var i=0,a=1;i < 10;i++) {
var t = a+i;
// do something with t
}
}
Chapter 9: Bandwidth and Performance Optimizations
217
Dot Notation and Property Lookups
Accessing objects and properties by dot notation is never efficient.


Pages:
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252