Avoiding inefficient loops

Nesting loops is the first thing to avoid when coding these types of structures in JavaScript.

Also, most of the time, using for-in loops is not a good idea since the engine has to create a complete list of the enumerable properties, which is not very efficient. Most times, a for loop will do the job perfectly. This is particularly true of performance-critical functions that are found along the critical path of the application.

Furthermore, beware of implicit object conversions when dealing with loops. Often, at first glance, it is difficult to see what is happening under the hood when repeatedly accessing a length property on an object for example. But there are cases where JavaScript will create an object on each ...

Get Mastering The Faster Web with PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.