-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
jQuery Design Patterns
By :
In this section, we will analyze some performance tips that are not jQuery-specific and can be applied to most JavaScript implementations.
When iterating over the items of an array or an array-like collection with a for loop, a simple way to improve the performance of the iteration is to avoid accessing the length property on every loop. This can easily be done by storing the iteration length to a separate variable, declared just before the loop or even along with it, as shown below:
for (var i = 0, len = myArray.length; i < len; i++) {
var item = myArray[i];
/*...*/
} Moreover, if we need to iterate over the items of an array that does not contain falsy values, we can use an even better pattern which is commonly applied for iterating over arrays that contain objects:
var objects = [{ }, { }, { }];
for (var i = 0, item; item = objects[i]; i++) {
console.log(item);
}In this case, instead of relying on the length property...
Change the font size
Change margin width
Change background colour