Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My JavaScript Object is too big

Tags:

javascript

I am creating a really big JavaScript object on page load. I am getting no error on firefox but on Internet Explorer I am getting an error saying:

Stop running this script ? A script on this page is causing your web browser to run slowly. If it continues to run, your computer might become unresponsive.

Is there any size limit for Javascript objects in Internet Explorer ? Any other solutions but not dividing the object?

like image 908
Kubi Avatar asked Jan 22 '23 05:01

Kubi


2 Answers

The key to the message your receiving is the "run slowly" part, which relates to time. So, your issue is not object size, but the time taken to construct the object.

To refine things even further, the issue is not the time taken to construct the object either. Rather, IE counts the number of javascript statements it executes, resetting this count when it executes an event handler or a setTimeout function.

So, you can prevent this problem by splitting your code into multiple pieces that run inside calls to setTimeout(...);

Here's an example that may push you in the right direction:

var finish = function(data) {
    // Do something with the data after it's been created
};

var data = [];
var index = 0;

var loop;
loop = function() {
  if (++index < 1000000) {
    data[index] = index;
    setTimeout(loop, 0);
  } else {
    setTimeout(function(){ finish(data); }, 0);
  }
}

setTimeout(loop, 0);
like image 65
John Fisher Avatar answered Jan 31 '23 14:01

John Fisher


The resources available to JavaScript are limited by the resources on the client computer.

It seems that your script is using too much processing time while creating that object, and the 'stop script' mechanism is kicking in to save your browser from hanging.

The reason why this happens on Internet Explorer and not on Firefox is probably because the JavaScript engine in Firefox is more efficient, so it does not exceed the threshold for the 'stop script' to get triggered.

like image 34
Daniel Vassallo Avatar answered Jan 31 '23 14:01

Daniel Vassallo