开发者

what is DOM js Memory Capacity?

开发者 https://www.devze.com 2023-02-05 21:19 出处:网络
I was wondering does anyone know what are the limits of dom 开发者_C百科for example. If I have a json object

I was wondering does anyone know what are the limits of dom 开发者_C百科for example.

If I have a json object

var json = {}

and this json object had its own objects inside, including arrays,other objects, or event plain text.

how many child objects can json hold if there is a limit, and also what if the object was dynamically increased in size would this ever display an error? because there is no more memory to handle it etc...


I don't believe there are hard limits, but I was curious to see what would happen, so I wrote a little ad hoc test script:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <script language="javascript">
            window.onload = function() {
                var data = [];
                var i = 0;

                var addData = function() {
                    var arr = [];
                    i++;

                    for(var j=0; j<10000; j++) {
                        arr.push({
                            id: i + j,
                            name: "item" + j,
                            number: i * j,
                            date: new Date(),
                            email: "long_email" + i + "@domain" + j + ".com"
                        });
                    }

                    data.push(arr);
                    document.getElementById("result").innerHTML = i.toString();
                    window.setTimeout(addData, 1);
                };

                addData();
            };
        </script>
    </head>
    <body>
        <div id="result"></div>
    </body>
</html>

The script builds a giant array. In each iteration, a smaller array is created with 10,000 objects and pushed to the main array. Each object contains 5 members: 2 numbers, 2 strings and 1 date. I count the number of iterations.

I tested this script on two computers, except for IE7 and IE8 for which I only had one computer available. The first computer runs Windows XP with 2GB ram, the second computer runs Windows 7 with 4GB ram.

I got the following results:

  • IE 7: the counter stopped at 52 before it froze. The browser became completely unresponsive. No warning was shown. I had to kill the process.
  • IE 8: the counter went as far as 108. The browser became completely unresponsive. No warning was shown. I had to kill the process.
  • Firefox 3.6: the counter went to 289 on the first computer and 580 on the second. At this moment a warning was shown (A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete.) Since the browser was very unresponsive, I stopped the script.
  • Opera 11: the counter went to 235 on the first computer and to 232 on the second. The browser was a little laggy. No warning was shown.
  • Chrome 8: the counter went to 327 on both computers, so it looks like a hard limit was reached. The browser was still very responsive. No warning was shown.


There are no limits in this case according to standards of JS, DOM or JSON.

If there is any limit, it highly depends on implementation and should be checked in browser's (or js engine's) documentation. And the only limit that I know is 1.8 Gb for heap in V8 (Chrome's JS engine).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号