Solved

Javascript: optimize loops

Posted on 2013-12-09
6
188 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
[X]
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
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

738 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