美文网首页
nodejs -- splite('') Array 最大限制

nodejs -- splite('') Array 最大限制

作者: 反者道之动001 | 来源:发表于2019-12-11 10:04 被阅读0次

字符串分割循环,转array,遇到最大内存分配限制

invalid array length Allocation failed - JavaScript heap out of memory

可以取巧,不用array。

看代码

var txt = 'string'

// split分割
var token = txt.split('')
for(var key in token){
        key = key | 0
        var v = token[key]
        console.log(v, key)
}

console.log('----')

// index自增
var txt_len = txt.length
var token = txt
for(var key = 0; key < txt_len; key++){
    var v = token[key]
    console.log(v, key)
}

因为txt是测试,字符串太小了,我运行一个680M,就会发现两个区别

txt.split('')的异常截图

image
改成index自增形式
image image

--END--

相关文章

网友评论

      本文标题:nodejs -- splite('') Array 最大限制

      本文链接:https://www.haomeiwen.com/subject/faqogctx.html