Javascript 字符串大小限制:对我来说是 256 MB - 所有浏览器都一样吗?

2024-02-21

我很好奇我能在 Javascript 中获得的最大字符串长度是多少,今天我在 Windows 7 中运行的 Firefox 43.0.1 上亲自测试了它。我能够构造一个具有长度的字符串2^28 - 1,但是当我尝试创建一个多一个字符的字符串时,Firebug http://getfirebug.com/向我展示了“分配大小溢出”错误,意味着字符串必须小于 256 MB。

对于所有浏览器、所有计算机、所有操作系统来说,这都是一样的吗?还是视情况而定?

我创建了以下代码片段来找出限制:

(function() {
    strings = ["z"];
    try {
        while(true) {
            strings.push(strings[strings.length - 1] + strings[strings.length - 1]);
        }
    } catch(err) {
        var k = strings.length - 2;
        while(k >= 0) {
            try {
                strings.push(strings[strings.length - 1] + strings[k]);
                k--;
            } catch(err) {}
        }
        console.log("The maximum string length is " + strings[strings.length - 1].length);
    }
})();

如果您运行不同的浏览器/操作系统,我想看看您的结果。我的结果是最大字符串长度为 268435455.

P.S.:我四处寻找答案,但我发现的最新主题是 2011 年的,所以我正在寻找更新的信息。


字符存储在16位上

当你看到那个时256*2**20字符在字符串中,这并不意味着分配了 256 MB 的内存。 JavaScript 将每个字符存储在两个字节上(因为它是由规范编码的 utf16)。

一句话关于ropes https://en.wikipedia.org/wiki/Rope_(data_structure)

当今的浏览器(甚至 IE)以高级方式存储字符串,最常见的是使用rope数据结构 https://en.wikipedia.org/wiki/Rope_(data_structure).

  • 绳索不需要分配一致的内存区域
  • 甚至可以删除重复的子字符串,这意味着s+s不一定使用两倍的内存s
  • 连接速度非常快
  • 元素访问有点慢

通过检查 IE 和 Chrome 中的一些运行,我想说它们都对字符串使用了一些惰性求值,并且偶尔会尝试扩展它们。运行以下代码片段后,没有一个浏览器使用的内存比以前更多。但如果我试图操纵存储的window.LONGEST_STRING在控制台中,IE 抛出内存不足错误,chrome 短暂冻结,并消耗大量内存(>2 GB)。

ps:在我的笔记本电脑上,IE11 的最大字符串大小为 4 GB,Chrome 为 512 MB

浏览器行为

IE11

Chrome47

确定最大字符串大小的更快算法

var real_console_log = console.log;
console.log = function(x) {
  real_console_log.apply(console, arguments);
  var d = document,b=d.body,p=d.createElement('pre');
  p.style.margin = "0";
  p.appendChild(d.createTextNode(''+x));
  b.appendChild(p);
  window.scrollTo(0, b.scrollHeight);
};


function alloc(x) {
    if (x < 1) return '';
    var halfi = Math.floor(x/2);
    var half = alloc(halfi);
    return 2*halfi < x ? half + half + 'a' : half + half;
}

function test(x) {
    try {
        return alloc(x);
    } catch (e) {
        return null;
    }
}

function binsearch(predicateGreaterThan, min, max) {
    while (max > min) {
        var mid = Math.floor((max + min) / 2);
        var val = predicateGreaterThan(mid);
        if (val) {
            min = mid + 1;
        } else {
            max = mid;
        }
    }
    return max;
}

var maxStrLen = binsearch(test, 10, Math.pow(2, 52)) - 1;
console.log('Max string length is:');
console.log(maxStrLen + ' characters');
console.log(2*maxStrLen + ' bytes');
console.log(2*maxStrLen/1024/1024 + ' megabytes');
console.log('');
console.log('Store longest string');
window.LONGEST_STRING = alloc(maxStrLen);

console.log('Try to read first char');
console.log(window.LONGEST_STRING.charAt(0));
console.log('Try to read last char');
console.log(window.LONGEST_STRING.charAt(maxStrLen - 1));
console.log('Try to read length');
console.log(window.LONGEST_STRING.length);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript 字符串大小限制:对我来说是 256 MB - 所有浏览器都一样吗? 的相关文章

随机推荐