标签:and com size ack 数组元素 回收 rip 基本 限制
var q = [ 5, 5, 1, 9, 9, 6, 4, 5, 8]; var b = [ "tie", "mao", "csdn", "ren", "fu", "fei" ];
[ 5, 5, 1, 9, 9, 6, 4, 5, 8, "tie", "mao", "csdn", "ren", "fu", "fei" ]
var c = q.concat( b ); q; // [5,5,1,9,9,6,4,5,8] b; // ["tie","mao","csdn","ren","fu","fei"]; c; // [5,5,1,9,9,6,4,5,8,"tie","mao","csdn","ren","fu","fei"]如您所见, c 是一个全新的数组, 表示 q 和 b 这两个数组的组合, 可是 q 和 b 如今没用了是吧?
q = b = null; // `q` and `b` 如今能够被垃圾回收了额?
假设数组都非常小,那自然没问题. 但对大型的数组,或须要多次反复处理时, 内存就被限制了, 它还须要进行优化.
循环插入
OK, 让我们把一个数组的内容加入到还有一个中试试,使用 Array#push() 方法:
// 将数组 `b` 插入 `q` for (var i=0; i < b.length; i++) { q.push( b[i] ); } q; // [5,5,1,9,9,6,4,5,8,"tie","mao","csdn","ren","fu","fei"] b = null;
// `q` into `b`: for (var i=q.length-1; i >= 0; i--) { b.unshift( q[i] ); } b; // [5,5,1,9,9,6,4,5,8,"tie","mao","csdn","ren","fu","fei"] q = null;
// `b` onto `q`: q = b.reduce( function(coll,item){ coll.push( item ); return coll; }, q ); q; // [5,5,1,9,9,6,4,5,8,"tie","mao","csdn","ren","fu","fei"] // or `q` into `b`: b = q.reduceRight( function(coll,item){ coll.unshift( item ); return coll; }, b ); b; // [5,5,1,9,9,6,4,5,8,"tie","mao","csdn","ren","fu","fei"]
// `b` onto `q`: q.push.apply( q, b ); q; // [5,5,1,9,9,6,4,5,8,"tie","mao","csdn","ren","fu","fei"] // or `q` into `b`: b.unshift.apply( b, q ); b; // [5,5,1,9,9,6,4,5,8,"tie","mao","csdn","ren","fu","fei"]
function combineInto(q,b) { var len = q.length; for (var i=0; i < len; i=i+5000) { // 一次处理5000条 b.unshift.apply( b, q.slice( i, i+5000 ) ); } }
标签:and com size ack 数组元素 回收 rip 基本 限制
原文地址:http://www.cnblogs.com/tlnshuju/p/6740554.html