Javascript 数组连接与字符串连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7296594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:40:39  来源:igfitidea点击:

Array Join vs String Concat

javascriptarraysperformancejoinconnection-string

提问by ajax333221

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];
}

采纳答案by AlienWebguy

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

ECMAScript 中的字符串连接速度更快。这是我创建的一个基准测试:

http://jsben.ch/#/OJ3vo

http://jsben.ch/#/OJ3vo

回答by Ryan Doherty

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.

我可以肯定地说使用 Array.join() 更快。我已经处理了几段 JavaScript 代码,并通过删除字符串操作以支持数组来显着提高性能。

回答by vitaly-t

From 2011 and into the modern day ...

从 2011 年到现代......

See the following joinrewrite using string concatenation, and how much slower it is than the standard implementation.

请参阅以下join使用字符串连接的重写,以及它比标准实现慢多少。

// Number of times the standard `join` is faster, by Node.js versions:
// 0.10.44: ~2.0
// 0.11.16: ~4.6
// 0.12.13: ~4.7
// 4.4.4: ~4.66
// 5.11.0: ~4.75
// 6.1.0: Negative ~1.2 (something is wrong with 6.x at the moment)
function join(sep) {
    var res = '';
    if (this.length) {
        res += this[0];
        for (var i = 1; i < this.length; i++) {
            res += sep + this[i];
        }
    }
    return res;
}

The moral is - do not concatenate strings manually, always use the standard join.

道德是 - 不要手动连接字符串,始终使用标准join.

回答by Lin

It depends:

这取决于:

Chromium 79.0.3945

铬 79.0.3945

Array Join is 30% slower

Array Join 慢 30%

Firefox 71.0.0

火狐 71.0.0

String Concat is 90% slower

String Concat 慢 90%

https://jsperf.com/lin-array-join-vs-string-concat

https://jsperf.com/lin-array-join-vs-string-concat

回答by Wilt

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.

根据这份名为“优化 JavaScript 代码”的Google 文档,字符串连接比数组连接慢,但显然这不适用于现代 Javascript 引擎。

I made a benchmark for the Fibonacci test examplethat they used in the document and it shows that concatenating (gluing) the string is almost 4x as fast as using Arrayjoin.

他们在文档中使用的斐波那契测试示例了一个基准测试,它表明连接(粘合)字符串的速度几乎是使用Arrayjoin.

回答by Matthias

Manual concatenation is faster, for a numeric array of fixed length.

对于固定长度的数字数组,手动连接速度更快。

Here's a JSPerf test that tests these two operations:

这是测试这两个操作的 JSPerf 测试

zxy.join('/')

// versus

zxy[0] + '/' + zxy[1] + '/' + zxy[2]

// given the array

zxy = [1, 2, 3]

// resulting in the string '0/1/2'

Results:Using Chrome 64.0.3282.186, Array.joinwas 46% slower.

结果:使用 Chrome 64.0.3282.186,Array.join慢了 46%。

回答by Melchia

The spread operator, written with three consecutive dots ( ... ), is new in ES6and gives you the ability to expand, or spread, iterable objects into multiple elements.

扩展运算符由三个连续的点 ( ... ) 组成,是 ES6 中的新功能,它使您能够将可迭代对象扩展或扩展为多个元素。

const books = ["Don Quixote", "The Hobbit", "Alice in Wonderland", "Tale of Two Cities"];
console.log(...books);

Prints: Don Quixote The Hobbit Alice in Wonderland Tale of Two Cities

版画:唐吉诃德 霍比特人爱丽丝梦游仙境 两个城市的故事