Geeks With Blogs

News Photo of Tanzim Saqib Tanzim Saqib is a Senior Developer, who spent half of his life on software and worked for many companies like #1 .NET controls provider Telerik Inc, #1 personalized Web 2.0 start-page like Pageflakes (acquired by LiveUniverse). He developed many projects ranging from banking solutions for Citibank, HSBC, Wamu, Wells Fargo etc. to Paperless Virtual University. He is industry's earliest and leading widget developer and as know as "Widget Master" to his peers.

He is a preacher of Microsoft technologies. While he jams with the latest additions to .NET, in his spare time he blogs at http://weblogs.asp.net/TanzimSaqib, maintains his personal website http://www.TanzimSaqib.com, leads .NET Research group. writes articles.

He is an easy going, fun loving, and passionate technology individual who is open to any kind of business opportunity and professional relationship. He currently lives in Bangladesh, but travels anywhere in the world on professional demand.

Email: me at TanzimSaqib dot com
Tanzim Saqib on .NET discovery Innovate. Create. Share.

In one of my earlier posts, I talked about DOM element accessing in a loop but forgot to talk about a very common, yet performance issue in AJAX. We often use code like the following:

var items = []; // Suppose a very long array 
for(var i=0; i<items.length; ++i)
    ; // Some actions
It can be a severe performance issue if the array is so large. JavaScript is an interpreted language, so when interpreter executes code line by line, every time it checks the condition inside the loop, you end up accessing the length property every time. Where it is applicable, if the contents of the array does not need to be changed during the loop's execution, there is no necessity to access the length property every time. Take out the length in a variable and use in every iteration:
var items = []; // Suppose a very long array 
var count = items.length;
for(var i=0; i<count; ++i)
    ; // Some actions
Posted on Thursday, January 10, 2008 6:52 AM JavaScript , Performance | Back to top


Comments on this post: ASP.NET AJAX Best Practices: Avoid using Array.length in a loop

# re: ASP.NET AJAX Best Practices: Avoid using Array.length in a loop
Requesting Gravatar...
Hey! - First off - great blog.

Just to clarify, in a compiled language like .NET is there any negative impact from accessing the .Length property in your for() clause...?

IE,
for(var i=0; i<items.length; ++i)...

S
Left by Sanjay on Feb 01, 2008 11:53 PM

# re: ASP.NET AJAX Best Practices: Avoid using Array.length in a loop
Requesting Gravatar...
Not sure, but compilers should be smart enough these days! :-)

You might want to dig deeper into CLR stuffs to find it out yourself.
Left by Tanzim Saqib on Feb 02, 2008 12:01 AM

# re: ASP.NET AJAX Best Practices: Avoid using Array.length in a loop
Requesting Gravatar...
Heya Tanzim

An even more compact syntax is to skip the declared var and use something like

for (var index = 0, len = myArray.length; index < len; ++index) { ... }

It's basically the same but I think it scopes len to *just* the loop and rids you of an extra var'd variable.

[I picked this syntax up from the prototype doco, but it's applicable anywhere you want to for loop through a large array.]

Cheers!
Dan

Left by Dan F on Feb 04, 2008 11:16 AM

# re: ASP.NET AJAX Best Practices: Avoid using Array.length in a loop
Requesting Gravatar...
Dan, you're right. Good catch. :-)
Left by Tanzim Saqib on Feb 04, 2008 11:22 AM

Your comment:
 (will show your gravatar)


Copyright © Tanzim Saqib | Powered by: GeeksWithBlogs.net