Javascript: optimize loops

Posted on 2013-12-09
Last Modified: 2013-12-09

I was just reading these posts:

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

function chunk(array, process, context){
        var item = array.shift();, 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.
Question by:Victor Kimura
  • 3
  • 3
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 500 total points
ID: 39706063
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 :)

Author Comment

by:Victor Kimura
ID: 39706339
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?
LVL 30

Accepted Solution

Alexandre Simões earned 500 total points
ID: 39706366

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...
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


Author Closing Comment

by:Victor Kimura
ID: 39706442
I get it. Thank you so much for the clear and thorough answers, Alex! =)
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39706796
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.


Author Comment

by:Victor Kimura
ID: 39707185
That's great, Alex. I'm looking forward to your site and updates on them. Keep me posted on your site. =)

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question