alex
alex

Reputation: 4924

How much data can a javascript global variable hold?

To enable a go back function with an ajax div i have create these simple functions and i was wondering how much data a .js global variable can hold??

    var dataAfterSearch; //global variable which holds our search results

function goBackAfterSearch() {
    /**
    *   function which displays the previous state
    *
    **/
    $.ajaxSetup ({
        cache: false
    });
    //alert("Previous Search" +dataAfterSearch);
    $('#result').html(dataAfterSearch);
     paginateIt();
}
function setDataAfterSearch(data)
{   
    /**
    * function to set the global dataAfterSearch
    *
    **/
    dataAfterSearch = data;
}

kind regards

Upvotes: 5

Views: 5397

Answers (4)

kanakamohan
kanakamohan

Reputation: 11

Ya there is an limit so far i observed, one of the method was returning a data more than 6MB size of data into java script variable but it could not handle such large amount of data in fact it was throwing error (JVM closed abruptly)i tried in mozilla and IE,this is what i observed.

Upvotes: 1

Ryan Emerle
Ryan Emerle

Reputation: 15821

There is no limit, the maximum size is browser/implementation specific.

You can test the limit by executing a script like this:

var str = "";
var sizeCount = 0;
while( true ) {
   str += "a";
   if( ++sizeCount >= 1048576 ) { // Show an alert for every MB
      alert( str.length );
      sizeCount = 0;
   }
}

I get an error in Chrome around 26MB.

Upvotes: 13

PanCrit
PanCrit

Reputation: 2718

a variable in javascript can hold an object or a string. An object is an unlimited data structure. Strings are limited only by the implementation (as Bob said), and shouldn't be a constraint for you.

Upvotes: 0

Bob
Bob

Reputation: 7971

Global variables aren't any different than non-global variables in what and how much they can hold. The specifications don't mention a limitation on string length (a string being what will be returned by AJAX), so I think it's implementation dependent.

I found this discussion on bytes.com where they mention some limitations they reached amongst different browsers.

Upvotes: 0

Related Questions