数组连接与字符串连接
哪种方法更快?
数组连接:
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("");
字符串连接:
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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
ECMAScript 中的字符串连接速度更快。这是我为向您展示而创建的基准:
http://jsben.ch/#/OJ3vo
String concatenation is faster in ECMAScript. Here's a benchmark I created to show you:
http://jsben.ch/#/OJ3vo
从 2011 年到现代...
请参阅以下使用字符串连接的
join
重写,以及它比标准实现慢了多少。道德是 - 不要手动连接字符串,始终使用标准
join
。From 2011 and into the modern day ...
See the following
join
rewrite using string concatenation, and how much slower it is than the standard implementation.The moral is - do not concatenate strings manually, always use the standard
join
.我可以肯定地说使用 Array.join() 更快。我编写了几段 JavaScript 代码,并通过删除字符串操作而使用数组来显着提高性能。
I can definitely say that using
Array.join()
is faster. I've worked on a few pieces of JavaScript code and sped up performance significantly by removing string manipulation in favor of arrays.2021 年测试
请参阅下面的代码。结果:
Firefox:在常规使用中,push+join 比字符串连接慢 80%。
Chrome:在常规使用中,push+join 比字符串连接慢 140%。
2021 test
See code below. Results:
Firefox: push+join is 80% slower than string concat, in regular usage.
Chrome: push+join is 140% slower than string concat, in regular usage.
当字符串数组已经存在时,
join
的速度要快得多。真正的比较是比较:对于少量迭代和字符串,使用推送和连接还是连接并不重要。然而,对于大量字符串,数组推送和连接在 Chrome 和 Firefox 中似乎更快。
以下是 10 到 1000 万个字符串的代码和测试结果:
Chrome:
Firefox:
要测试的代码:
join
is way faster when the array of strings already exists. The real comparison would be comparing:For small number of iteration and strings, it does not matter whether you use push-and-join or concatenate. However, for large number of strings, array push and join seems to be faster in both chrome and firefox.
Here is the code and the test results for 10 to 10 million strings:
Chrome:
Firefox:
Code to test:
这取决于:
铬 79.0.3945
Firefox 71.0.0
https://jsperf.com/lin-array-连接与字符串连接
It depends:
Chromium 79.0.3945
Firefox 71.0.0
https://jsperf.com/lin-array-join-vs-string-concat
根据这份标题为的 Google 文档优化 JavaScript 代码的字符串连接比数组连接慢,但显然对于现代 Javascript 引擎来说并非如此。
我为他们在文档中使用的斐波那契测试示例制作了基准,它显示了连接(粘合)字符串的速度几乎是使用
Array
join
的 4 倍。According to this Google document titled 'Optimizing JavaScript code' string concat is slower then array join but apparently this is not true for modern Javascript engines.
I made a benchmark for the Fibonacci test example that they used in the document and it shows that concatenating (gluing) the string is almost 4x as fast as using
Array
join
.对于固定长度的数值数组,手动连接速度更快。
这是一个测试这两个操作的 JSPerf 测试:
结果:使用 Chrome 64.0.3282.186,
Array.join
速度慢了 46%。Manual concatenation is faster, for a numeric array of fixed length.
Here's a JSPerf test that tests these two operations:
Results: Using Chrome 64.0.3282.186,
Array.join
was 46% slower.我认为这不仅是性能问题,而且是内存问题
,因为字符串是不可变的,这意味着每次连接字符串时,都会在内存中创建一个额外的字符串,
但数组是可变的,这意味着它将保持相同的内存地址,
当然它也是取决于语言,一般来说,数组是更好的解决方案
i think it is not only performance problem, but memory too
as string is immutable, means every time you concat a string, a extra string create in your memory
but array is mutable, mean it will keep the same memory address
of course it is also depend on languages, in general , array is a better solution
扩展运算符由三个连续的点 ( ... ) 编写,是 ES6 中的新增功能,它使您能够将可迭代对象扩展或扩展为多个元素。
版画: 唐吉诃德 霍比特人 爱丽丝梦游仙境 两座城市的故事
The spread operator, written with three consecutive dots ( ... ), is new in ES6 and gives you the ability to expand, or spread, iterable objects into multiple elements.
Prints: Don Quixote The Hobbit Alice in Wonderland Tale of Two Cities