Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Javascript: optimize loops

Hi,

I was just reading these posts:

http://jonraasch.com/blog/10-javascript-performance-boosting-tips-from-nicholas-zakas
http://www.nczonline.net/blog/2009/01/13/speed-up-your-javascript-part-1/

In particular, my question alludes to the second post from nczonline.net. Supposedly, this function that Zakas, the author created will give a boost in performance in loops:

function chunk(array, process, context){
    setTimeout(function(){
        var item = array.shift();
        process.call(context, item);

        if (array.length > 0){
            setTimeout(arguments.callee, 100);
        }
    }, 100);
}

Open in new window


Two questions:
1) I'm wondering they there is a timer set to 100ms. Doesn't that slow down the execution?

2) What is the arguments.callee? What object is the arguments from?

Much thanks.
0
Victor Kimura
Asked:
Victor Kimura
  • 3
  • 3
2 Solutions
 
Alexandre SimõesManager / Technology SpecialistCommented:
This is a way to handle large loops.
If you want to loop through a bit about of data and that process takes several seconds to finish, during this time the UI will be blocked.

What that function does is processing each item with a 100ms gap before processing the next.
This gives enough time for the UI thread to "sneak in" and do some pending rendering or action the user might have invoked meanwhile.

This can be also a good technique to update a progress bar or percentage.
If you don't let the UI "breath" within the loop you can update the progress meter but the change won't be visible until the loop is finished.

btw, of course this will be overall slower than processing the whole loop without the time out... it's just a matter of not blocking the UI... too much :)
0
 
Victor KimuraAuthor Commented:
Oh, I see. Thanks, AlexCode! Makes sense now. =)

Just wondering now regarding the second question:

2) What is the arguments.callee? What object is the arguments from?
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments/callee

He uses this to make the function call itself.
This way you don't need to know the call of the function you're in...

Remember that the whole idea of this code is to process a list of items in a sequence, by the same function separating each item processing by 100ms.

This is a very ingenious way of nonblocking the UI. Be aware that 99.99% of the times you might need to use this it's because there's something wrong with your design...

Use these trick with a huge dose of sense...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Victor KimuraAuthor Commented:
I get it. Thank you so much for the clear and thorough answers, Alex! =)
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
I highly recommend the following book:
High Performance Javascript

Shortly my website will be online and you'll have a lot of resources on this subject also.

Cheers!
0
 
Victor KimuraAuthor Commented:
That's great, Alex. I'm looking forward to your site and updates on them. Keep me posted on your site. =)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now