tags:

views:

199

answers:

4

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?

+1  A: 

that is not because of the size but because of the big quantity of loops you are executing and the big execution time. if you cut it into smaller parts it should work ok.

Try lowering the complexity of functions your running. Can you post it so that we can look at the loops and try to help?

Edit:

I supose you want to do all that on the client side for some reason. The code seems to need to much execution to be runing on the client side.

Can't you do the calculations on the server side? If this is all to initialize the object, you can cache it to avoid reprocessing and just send a generated json to the javascript side.

It does seem cachable

fmsf
I am sorry it is too complex to post it here. But basically it is an object array including 700 objects. These objects include 4 properties one of which is including up to 6 other kind of properties.
Kubi
i did post it :)
Kubi
no we don't want to make it on serverside ! Generated json becomes around 10mbs.
Kubi
+2  A: 

You must be using big loops or some recursive logic in the code. It really doesn't depend on the size of the object—it depends on the CPU resources it uses (memory, processor, etc.).

Shoaib Shaikh
+3  A: 

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);
John Fisher
could you check my code ?
Kubi
@Kubi: Your code should convert rather easily. If you need help after trying a bit, post it here on SO and see what you get.
John Fisher
I think I couldn't get the idea. I tried to put setTimeout before allPlans.push if allPlans.length is greater than 1000.
Kubi
You need to take the content of your "while (index < dizi.length - 1) {" loop and put it inside the "loop = function() {" in my example. The part that concerns me is what you do with the result that is returned. If something actually uses the returned result, then you'll need to place that code inside something like the "finish" function.
John Fisher
the result that is returned is the object which I want to use as a global object. I want to initialize in the page load and use it in the whole page lifecycle. What I did is to add the content of the loop into the var loop = function as you said but I got other errors while processing other functions which are related the returend object.
Kubi
@Kubi: Instead of returning the result, you'll need to call the next function from the "finish" method. setTimeout() is designed to do work outside the current function's control (after the current function and its callers have finished their work). So, any function depending upon the results from this code will need to be triggered from the "finish" function in my example.
John Fisher
+2  A: 

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.

Daniel Vassallo