3

Possible Duplicate:
Javascript Memory Limit

I'm working on creating html page using client side javascript which will load around 150mb of XML data file on page load. When the file size was around 5 mb, it took 30 seconds to load whole data into an array. But when I changed file to 140 mb, the page is not responding in firefox and crashing abruptly in chrome. My snippet to load data will process on every individual tag in xml. My question is, is there any limited heap size for javascript? Any academic article resource is preferable to emphasize my research.

$(document).ready(function () {
        // Open the xml file
        $.get("xyz.xml", {}, function (xml) {
            // Run the function for each  in the XML file
             $('abc', xml).each(function (i) {
                a = $(this).find("a").text();
                b = $(this).find("b").text();
                c = $(this).find("c").text();
                ab = $(this).find("ab").text();
                bc = $(this).find("bc").text();
                cd = $(this).find("cd").text();
                de = $(this).find("de").text();
                // process data
              dosomething(a,b,c,ab,bc,cd,de);
                   }); }); });
Community
  • 1
  • 1
FairDinkum82
  • 167
  • 2
  • 11
  • I've read files that were hundreds of megabytes with javascript before and had no issues, I was not doing it in a browser though. – Benjamin Gruenbaum Jan 30 '13 at 22:33
  • @BenjaminGruenbaum were you instantiating a DOM from an XML file that big? – Pointy Jan 30 '13 at 22:34
  • @Pointy, not sure if it would quality as a dom per se, I was using JSDOM and jQuery on node.js. It _was_ slower than CsQuery on C# but it was still reasonable – Benjamin Gruenbaum Jan 30 '13 at 22:36
  • 1
    See this answer also http://stackoverflow.com/questions/4833480/have-i-reached-the-limits-of-the-size-of-objects-javascript-in-my-browser-can-ha/4833756#4833756 – Ruan Mendes Jan 30 '13 at 22:41

2 Answers2

2

I don't know of any limits. I've been able to load even a 1Gb file. Yes, it was slow to load initially and everything ran slowly because most of the memory will be paged.

However, there are problems with trying to load a single JavaScript object that is that big, mostly because the parsers can't parse an object that is too big. See Have I reached the limits of the size of objects JavaScript in my browser can handle?

For that case, the solution was to break up the creation of the JavaScript object into multiple stages rather than using a single literal statement.

Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
1

Because it's to much to improve. First of all i'd like to recommend you a post 76 bytes for faster jQuery. So, relying on that replace your $(this) on $_(this). it will save you a lot memory and time!!

If you don't want to use single jQuery object, please cashe you variable like that:

$('abc', xml).each(function (i) {
   var $this = $(this);
   a = $this.find("a").text();
   ....
});

and you can provide your dosomething function to try to improve it

Vadim Ivanov
  • 633
  • 1
  • 7
  • 16
  • That won't help here. (I don't think the suggestion of that blog post is a good one in *any* situation, really.) – Pointy Jan 30 '13 at 22:33
  • (I should say that I *doubt* it'd help; I suspect the memory problem stems from just building the DOM from an XML file that big.) – Pointy Jan 30 '13 at 22:41
  • I agree, that it's not a javascript job to parse xml file that big. Better to parse on server side and then just get the result – Vadim Ivanov Jan 31 '13 at 11:14
  • What the hell does a random javascript library have to do with memory limitations for the language and runtime environment? jQuery is a simple toy of a library, its not javascript - so when responding to JS questions -- dont include jQuery. Do you see people answering C and ASM question with visual basic snippets? – Jon Lennart Aasenden Feb 19 '17 at 01:16
  • @JonLennartAasenden jQuery is javascript, and jQuery is very slow and memory inefficient. So, this is actually a "sort-of" valid answer. – Jack G Dec 10 '17 at 16:00
  • jQuery is a javascript library, what i meant was that people should learn vanilla javascript first and frameworks afterwards. Its like saying "SDL is C" .. no its not, SDL is a framework written in C to simplify things. But unless you master C well first it will be hard to produce good quality code. Trying to teach kids javascript is almost hopeless. Thankfully schools are going back to proper coding, object pascal and C/C++ so they get a solid education. No disrespect, people have no idea how much they damage education when they mix these up – Jon Lennart Aasenden Dec 22 '17 at 05:20