Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Javascript: optimize loops

Posted on 2013-12-09
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 2000 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 2000 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...
Independent Software Vendors: 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!


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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

704 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