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

2019-12-11  本文已影响0人  反者道之动001

字符串分割循环,转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--

上一篇 下一篇

猜你喜欢

热点阅读