1

I have a very large JavasScript for loop running on one of my pages. It is so large that it is causing an IE popup in old versions that says something like: 'A script of this page is making it run slow, do you want to terminate it?'

I'm confident that I have optimized it as much as possible so now i'm looking for alternate ways to do it.

I'm wondering whether it would be beneficial for me to create an interval that ran the first 100 iterations, then the second 100 iterations, then the third and so on until all the iterations were complete? Would this prevent the IE popup claiming that the scripts are running too slow?

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
user1636130
  • 1,615
  • 5
  • 29
  • 47
  • Javascript is single threaded so running something takes as long it takes, it does'nt go faster if you split it up (unless you're using workers), but it could make the browser more responsive, so that it does'nt hang for the duration of the loop, but it wont be faster all in all. – adeneo Apr 05 '13 at 09:41
  • What are you trying to do exactly? Usually you shouldn't be doing so many calculations. – MMM Apr 05 '13 at 09:43
  • Thanks, I guess that I didn't phrase my question in the best way. I am more interested in preventing the popup at this point than making the JavaScript itself faster. Do you think that the hanging that Adeneo mentioned is the cause of the popup? – user1636130 Apr 05 '13 at 09:45
  • Are the loops recursively dependent on each previous loop? We really need to see the loop in order to help you with optimisation. – marksyzm Apr 05 '13 at 09:47
  • Marksyzm, the loop does not need to be ran in any specific order. It is copying values from an extremely large number of textboxes and storing them in an array. As long as the loop iterates over all the textboxes it is fine. – user1636130 Apr 05 '13 at 09:50

3 Answers3

0

Javascript is single threaded, so that's the main obstacle.

If you want to perform a lot of calculations, preferably you should use Workers, however those are only supported by modern browsers. This so that your calculations don't slow down the UI, which should be in the main thread. This is a common practice when building desktop apps.

Splitting the calculation might be a good idea if you don't mind it taking a lot longer than it should. That should prevent the popup from appearing, provided you time it right.

You should also perhaps reconsider the need of performing so many calculations on the client side.

MMM
  • 7,221
  • 2
  • 24
  • 42
  • Thanks for your reply. I've building an internal app for a company still using old versions of IE so I can't rely on Workers. I'd love to perform these calculations server side but I am very limited by the software I'm being forced to use! I don't mind the code taking longer than it should so perhaps I will split it up. Thanks again. – user1636130 Apr 05 '13 at 09:55
  • Have you considered Java applets? Yes, they're ugly, but if you want to perform a lot of calculations at least that works in a separate thread and is (potentially) supported by older IEs. – MMM Apr 05 '13 at 09:56
0

You could use this: multithreading. it's not really multithreading, it's just playing with timers. It will prevent the popup. But I am not sure if it is necessary in this case, unless you're making a full-on game.

errieman
  • 1,849
  • 1
  • 12
  • 9
  • this is exactly what the OP suggests as a possible workaround in the question. – Alnitak Apr 05 '13 at 09:54
  • [The example from the article in jsfiddle](http://jsfiddle.net/FzrCt/) (because I can't edit it into the post for lack of code) – errieman Apr 05 '13 at 10:20
0

The "script too long" browser popup will appear any time that your code doesn't yield control back to the browser's main event processing loop quickly enough.

Using setTimeout as you suggest in the question to trigger your calculations in batches would indeed prevent the popup from appearing.

Yielding back to the event loop periodically will also allow the browser to remain responsive to other UI events - you could (for example) offer a "cancel" button. I would suggest that you try to yield at least once a second, if not somewhat more frequently than that.

Alnitak
  • 334,560
  • 70
  • 407
  • 495