5

I created a brute-force like script which basically needs to check more than 27,000 options, and after each check displays the result inside a div.

The script is coded correctly and if I lower the number of options it works sufficiently well, but if I have many options, after a few seconds, a window pops up telling me that my script is unresponsive. How can I make it responsive while checking this many options.

Oh and I almost forgot, it displays data (which is displayed after every check) only when that pop-up window appears (kinda weird).

Ry-
  • 218,210
  • 55
  • 464
  • 476
AleksaIl
  • 85
  • 1
  • 5
  • 27000 is too many option checks for JavaScript besides Your coding structure might be the reason of slowing down. It's better to show us some code. and use timing functions like `setTimeout` instead of loops. – Siamak Motlagh May 11 '13 at 16:47
  • Partition your work into smaller steps, after a step completes, call `setTimeout(nextStep, 1)`. – DCoder May 11 '13 at 16:47
  • 5
    You might want to look into [Web workers](http://www.html5rocks.com/en/tutorials/workers/basics/) – Lix May 11 '13 at 16:48
  • Thanks Lix, I think web workers will do the trick. – AleksaIl May 11 '13 at 16:51
  • @ale - awesome! :P When you figure out how to use them - tell me how! I haven't got round to playing with them yet ;) – Lix May 11 '13 at 17:19
  • Because Script runs of your browser, and when you give too much load, It still works but becomes overloaded. It is not a bug, it is like `stack overflow` in Recursion calling in C, not exactly though. – Muthu Ganapathy Nathan May 11 '13 at 16:47

1 Answers1

1

Asynchronous batch processing may solve your problem:

var options = ...; // your code

// I assume you are using something like this
function processAll() {
  for(var i=0; i<options.length; ++i) ... // causes unresponsivity
}

// try to use this instead
function batchProcessing(from) {
  if(from >= options.length) return;
  var to = Math.min(1000, options.length-from);
  for(var i=from; i<from+to; ++i) ... // your code
  // run the next batch asynchronously, let the browser catch the breath
  setTimeout(batchProcessing.bind(null, from+1000));
}
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169