Solved

Javascript: optimize loops

Posted on 2013-12-09
6
169 Views
Last Modified: 2013-12-09
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
Comment
Question by:Victor Kimura
  • 3
  • 3
6 Comments
 
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 :)
0
 

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?
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39706366
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Closing Comment

by:Victor Kimura
ID: 39706442
I get it. Thank you so much for the clear and thorough answers, Alex! =)
0
 
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.

Cheers!
0
 

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. =)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now