how much data can javascript in the browser hold?

前端 未结 4 1336
猫巷女王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:10

    Again, my machine with 16GB of RAM. I can watch the browser RAM usage climb as it increases, so I would assume it's limited by RAM as well.

    IE crapped out at 16,840,000
    Chrome at 14,850,000
    Firefox 32,890,000
    Safari recycles itself around 8,720,000 (LOL @ Apple)
    

    Here is a screenshot of memory usage and firefox http://screencast.com/t/3Xl31yGgHWC

    0 讨论(0)
  • 2020-12-30 08:11

    I am ON Chrome and i tried your test and it hangs on the same 14850000 you mentioned even if i only have 2gb of ram and i am running a VM with windows inside my linux installation that i gave 700Mb of ram so i guess yes chrome has a limit

    0 讨论(0)
  • 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:

    0 讨论(0)
  • 2020-12-30 08:27

    On a 32-bit machine your upper limit will always be 4GB, no if ands or buts. In practice, it will be wildly different machine to machine (not unusual to have 10+ tabs open)

    Best way to go: keep only the data your user is actively working on or anything you can't quickly retrieve from the server handy. Everything else, get when the user asks for it.

    0 讨论(0)
提交回复
热议问题