js数组复制速度测试220320
执行600000次
用 srcAr.concat() 用时 16 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 srcAr.slice() 用时 17 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 Array.from(srcAr) 用时 17 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 […srcAr] 用时 12 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 Object.assign([],srcAr) 用时 1985 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 JSON.parse(JSON.stringify(srcAr)) 用时 549 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
第二次用 srcAr.slice() 用时 16 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
第二次用 srcAr.concat() 用时 15 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 newAr=new Array(); for(let i=0;i43 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 newAr=new Array(); for(let i=0;i36 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 newAr=[]; for(let i=0;i32 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 newAr=[]; for(let i=0;i33 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 newAr=Array(srcAr.length); for(let i=0;i17 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
用 newAr=new Array(srcAr.length); for(let i=0;i23 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
第三次用 srcAr.concat() 用时 9 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
第三次用 srcAr.slice() 用时 14 毫秒
newAr=[0,1,2,3,4,5,6,7,8,9,十,十一,十二,十三,十四,十五]
测试代码
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"/><title></title><style>
#BodyH{display:flex; align-items:center; justify-content:center;}
</style><script>
const ge=window.dgebi=function(e,s){if(e&&e.constructor===String){let n=e;e=document.getElementById(e);e=e||document.querySelector(n);}if(e&&s){e.newE=e.newChild=s;e.appendChild(s);}return e;},
dcept=(p,tn="DIV")=>{let e=document.createElement(tn);e.idfine=(i)=>{e.id=i;return window[i]=e;};e.htm=(h)=>{e.innerHTML=h;return e;};dgebi(p,e);return e;} ;
if(!document.title.length)(function(){var str=document.URL; str=str.substring( str.lastIndexOf("/")+1 , str.length-5); str=decodeURI(str); document.title=str;})();
window.dwbr=function(a0=1){let ar=arguments,br=dwbr.br; if(Number.isFinite(a0)){br(a0);return;} for(let a of ar)document.write(a.replace(/\n/g,"<br/>"));br();}; dwbr.br=function(n=1){for(let c=0;c<n;c++)document.write("<br/>");};
window.childIdvar=function(q){for(let p of document.querySelectorAll(q))for(let c of p.children)if(c.id&&c.id.length>0)window[c.id]=c;};
</script><script>
</script></head><body style="margin:0; font-size:16px; "><header id="BodyH">
<label style="font-size:3rem; "></label><script>{let s=document.currentScript; let l=s.previousElementSibling; if(!l.innerHTML.length)l.innerHTML=document.title}</script>
</header><div id="BodyB">
<div id="d1" style="font-size:20px;"></div>
</div><footer id="BodyF"></footer><style id="LastStyleTag"></style><script>const BodyH=dgebi("BodyH") , BodyB=dgebi("BodyB") , BodyF=dgebi("BodyF") , LastStyleTag=dgebi("LastStyleTag") , LastStyleSheet=LastStyleTag.sheet;
let d1=dgebi("d1"); for(let t=0;t<60;t++)dcept(d1,"div");
{
let dc=d1.children;
let srcAr=[0,1,2,3,4,5,6,7,8,9,'十','十一','十二','十三','十四','十五'] , newAr=null , ms , ci=0 , count=600 * 1000;
function showNewAr(){dc[ci++].innerHTML="newAr=["+newAr+"]<br/>";}
dc[ci++].innerText="执行"+count+"次";
ms=Date.now(); for(let t=0;t<count;t++){newAr=srcAr.concat();} dc[ci++].innerHTML="用 srcAr.concat() 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=srcAr.slice();} dc[ci++].innerHTML="用 srcAr.slice() 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=Array.from(srcAr);} dc[ci++].innerHTML="用 Array.from(srcAr) 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=[...srcAr];} dc[ci++].innerHTML="用 [...srcAr] 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=Object.assign([],srcAr);} dc[ci++].innerHTML="用 Object.assign([],srcAr) 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=JSON.parse(JSON.stringify(srcAr));} dc[ci++].innerHTML="用 JSON.parse(JSON.stringify(srcAr)) 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=srcAr.slice();} dc[ci++].innerHTML="第二次用 srcAr.slice() 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=srcAr.concat();} dc[ci++].innerHTML="第二次用 srcAr.concat() 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=new Array(); for(let i=0;i<srcAr.length;i++)newAr.push(srcAr[i]); } dc[ci++].innerHTML="用 newAr=new Array(); for(let i=0;i<srcAr.length;i++)newAr.push(srcAr[i]); 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=new Array(); for(let i=0;i<srcAr.length;i++)newAr[i]=srcAr[i]; } dc[ci++].innerHTML="用 newAr=new Array(); for(let i=0;i<srcAr.length;i++)newAr[i]=srcAr[i]; } 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=[]; for(let i=0;i<srcAr.length;i++)newAr.push(srcAr[i]); } dc[ci++].innerHTML="用 newAr=[]; for(let i=0;i<srcAr.length;i++)newAr.push(srcAr[i]); 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=[]; for(let i=0;i<srcAr.length;i++)newAr[i]=srcAr[i]; } dc[ci++].innerHTML="用 newAr=[]; for(let i=0;i<srcAr.length;i++)newAr[i]=srcAr[i]; } 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=Array(srcAr.length); for(let i=0;i<srcAr.length;i++)newAr[i]=srcAr[i]; } dc[ci++].innerHTML="用 newAr=Array(srcAr.length); for(let i=0;i<srcAr.length;i++)newAr[i]=srcAr[i]; } 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=new Array(srcAr.length); for(let i=0;i<srcAr.length;i++)newAr[i]=srcAr[i]; } dc[ci++].innerHTML="用 newAr=new Array(srcAr.length); for(let i=0;i<srcAr.length;i++)newAr[i]=srcAr[i]; } 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=srcAr.concat();} dc[ci++].innerHTML="第三次用 srcAr.concat() 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
ms=Date.now(); for(let t=0;t<count;t++){newAr=srcAr.slice();} dc[ci++].innerHTML="第三次用 srcAr.slice() 用时 "+"<strong class='ms' >"+(Date.now()-ms)+"</strong>"+" 毫秒"; showNewAr();
}
</script></body></html>