gpt4 book ai didi

javascript - 未捕获的范围错误: Callstack exceeded when trying to convert Int8Array to String/JSON

转载 作者:行者123 更新时间:2023-12-03 00:21:02 25 4
gpt4 key购买 nike

我正在使用 WebAssembly 获取 JSON 数据,并将其保存到 IndexedDB。我可以设法从 IndexedDB 获取它作为 Int8Array,并将其转换为字符串,然后在它足够小时解析为 JSON,但是当数据太大时会出现错误:

Uncaught RangeError: Maximum call stack size exceeded

控制台日志截图:

Console

负责的代码位:

function ConvertToJSON(result){
console.log("= Converting to JSON =");
console.log(result);
let int8View = new Int8Array(result[0]);
console.log(int8View);
let str = String.fromCharCode.apply(String, int8View); //RangeError when array too large...
let json = JSON.parse(str);
console.log(json);
AddJSONToPage(json);
}

有办法避免这个问题吗?我是否需要将数组拼接成更小的位并进行转换,然后将其粘贴在一起,如果需要,我该如何处理?

最佳答案

根据MDN ,函数的硬编码限制为传递给函数的参数为​​ 65,536 个。由于您的 Int8Array 拥有超过 600,000 个元素,您的 apply 调用会由于提供的参数比实际数量多 10 倍而终止。您可以按照您所说的方式将其拆分,但您也可以在 Int8Array 上调用 reduce 并一次性完成所有操作:

int8View.reduce((arr, int8) => arr.concat( String.fromCharCode(int8) ), []);

关于javascript - 未捕获的范围错误: Callstack exceeded when trying to convert Int8Array to String/JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54313652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com