JavaScript Memory Limit

Ever wonder what is the maximum amount of memory Javascript is able to allocate? I’ve been considering a Javascript app requiring a decent amount of memory and wanted to find out the answer to that question. After searching on Google without luck, I decided it would be easier to code my own test. Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

<h1 id="max_mem">Max Memory = ?</h1>


<script language="javascript">
var kilo_str = ''
for(var i=0; i<1024; i++){
  kilo_str += 'a';
}
var large_str = '';
var size = 1;

while(true){
  large_str += kilo_str
  var x=document.getElementById("max_mem");
  x.innerHTML = "max memory = " + size + " kb";
  size++;

}
</script>

Just stick that in an HTML page and run the page. But, before you do so, please be aware that it may crash your browser. I ran this in Firefox and it would present me periodically with a popup asking if I wanted to stop the unresponsive script. After letting it continue through several iterations of the popup, I stopped the script and the max memory shown on the screen was 20480 KB (~20 MB). If I had more patience, then I could come up with a more accurate figure, but it looks like Firefox’s Javascript interpreter can allocate at least 20 MB, which is pretty fantastic! I would bet that the amount varies per browser though. Please note that this experiment also assumes that each character is stored as 1 byte.


About this entry