阵列加入VS字符串连接

阵列加入VS字符串连接

本文介绍了阵列加入VS字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种方法更快?

阵列加入:

  VAR str_to_split =A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q, R,S,T,U,v,W,X,Y,Z;
变种myArray的= str_to_split.split(,);无功输出= myarray.join();


字符串连接:

  VAR str_to_split =A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q, R,S,T,U,v,W,X,Y,Z;
变种myArray的= str_to_split.split(,);VAR输出=;
对于(VAR I = 0,LEN = myarray.length; I< LEN,我++){
    输出+ = myArray的[I]
}


解决方案

字符串concatination是ECMAScript中更快。下面是我创建的告诉你一个风向标:

http://jsperf.com/array-join-vs-string-connect

Which method is faster?

Array Join:

var str_to_split = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
var myarray = str_to_split.split(",");

var output=myarray.join("");


String Concat:

var str_to_split = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
var myarray = str_to_split.split(",");

var output = "";
for (var i = 0, len = myarray.length; i<len; i++){
    output += myarray[i];
}
解决方案

String concatination is faster in ECMAScript. Here's a benchmark I created to show you:

http://jsperf.com/array-join-vs-string-connect

这篇关于阵列加入VS字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:38