Posted By

iTony on 08/12/09


Tagged

javascript arrays Threading


Versions (?)

Performance in Array Processing


 / Published in: JavaScript
 

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

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

  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. }

Report this snippet  

You need to login to post a comment.