Performance in Array Processing


/ Published in: JavaScript
Save to your folder(s)

This method is better suited for processing large arrays in the smallest amount of time without affecting the user experience.


Copy this code and paste it in your HTML
  1. //Copyright 2009 Nicholas C. Zakas. All rights reserved.
  2. //MIT Licensed
  3. function timedChunk(items, process, context, callback){
  4. var todo = items.concat(); //create a clone of the original
  5.  
  6. setTimeout(function(){
  7.  
  8. var start = +new Date();
  9.  
  10. do {
  11. process.call(context, todo.shift());
  12. } while (todo.length > 0 && (+new Date() - start < 50));
  13.  
  14. if (todo.length > 0){
  15. setTimeout(arguments.callee, 25);
  16. } else {
  17. callback(items);
  18. }
  19. }, 25);
  20. }

URL: http://www.nczonline.net/blog/2009/08/11/timed-array-processing-in-javascript/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.