how much data can javascript in the browser hold?

前端 未结 4 1352
猫巷女王i
猫巷女王i 2020-12-30 07:42

I have created a JSFiddle to see how much data I can push into my browser.

The link is http://jsfiddle.net/GWxAk/

The code is simple. It\'s just trying to p

4条回答
  •  滥情空心
    2020-12-30 08:19

    Lets assume UTF8, meaning your 'a' is 2 bytes/8bits.

    • 14,850,000 * 300 = 4455000000 characters

    • 14850000 * 300 * 2 = 8,910,000,000 bytes

    • 8910000000 /1024 = 8,701,171.875 KB
    • (8910000000 /1024 ) / 1024 = 8,497.238159179688 MB
    • ((8910000000 /1024 ) / 1024 ) / 1024 = 8.298084139823914 GB

    As such we can surmise from your test that the maximum length of a string in Chromes JS engine is 4,455,000,000 characters, or ~ 8.3 GB in memory.

    But ofcourse this is not what's happening. You only have 4GB of RAM yet ~4298MB has appeared out of nowhere according to the figures, and there's the structures of the array variable itself and the java VM and chrome itself ot account for etc etc

    Not to mention that you're pushing s+count not s on its own, so the length of the string being added is rising as the number of digits in count increases. If s was the same, then its likely the value would be interned to save memory by the V8 engine. For reference, the number of additional characters added because of the count variable, and due to it's non linear increase in length, is 9,7438,889 characters or 185.85MB of data.

    So something else must be happening here.

    As for the limits of the V8 JS engine:

    http://code.google.com/p/v8/issues/detail?id=847

    The 32bit memory address space is the upper limit, and for 64bit, that link suggest ~1.9GB although it's very much likely to be the upper limit of what your OS can support and is physically available.

    So to summarise:

    • 32bit will always be an upper bound, not of your specific js variable, but of the entire bundle of js VM, renderer, page contents, etc
    • Your test is not quite reliable as the items it is counting are not identical
    • If they were identical, you would fall foul of special case handling of strings

    edit:

提交回复
热议问题