Array Join vs String Concat

时间:2011-09-04 00:04:32

标签: javascript arrays performance join connection-string

哪种方法更快?

阵列加入:

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

9 个答案:

答案 0 :(得分:42)

ECMAScript中的字符串连接速度更快。这是我为您展示的基准:

http://jsben.ch/#/OJ3vo

答案 1 :(得分:8)

我可以肯定地说使用Array.join()更快。我已经研究了一些JavaScript代码,并通过删除字符串操作来支持数组,从而大大加快了性能。

答案 2 :(得分:7)

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

使用字符串连接查看以下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;
}

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

答案 3 :(得分:2)

根据this Google document titled 'Optimizing JavaScript code'字符串concat比数组连接慢,但显然现代Javascript引擎不是这样。

我在文档中使用了a benchmark for the Fibonacci test example,它表明连接(粘合)字符串的速度几乎是使用Array join的4倍。

答案 4 :(得分:1)

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

Here's a JSPerf test that tests these two operations

zxy.join('/')

// versus

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

// given the array

zxy = [1, 2, 3]

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

结果:使用Chrome 64.0.3282.186,Array.join的速度降低了46%。

答案 5 :(得分:1)

当字符串数组已经存在时,'Join' 会更快。真正的比较是比较:

  1. 将元素推入数组,然后连接它们以构建字符串
  2. 每次连接字符串而不使用数组

对于少量的迭代和字符串,使用push-and-join还是concatenate都没有关系。但是,对于大量字符串,chrome 和 firefox 中的数组推送和连接似乎更快

这是代码和 10 到 1000 万个字符串的测试结果:

铬:

strings 10
join-only: 0.01171875 ms
push-join: 0.137939453125 ms
concatenate: 0.01513671875 ms
strings 100
join-only: 0.01416015625 ms
push-join: 0.13427734375 ms
concatenate: 0.0830078125 ms
strings 1000
join-only: 0.048095703125 ms
push-join: 0.47216796875 ms
concatenate: 0.5517578125 ms
strings 10000
join-only: 0.465087890625 ms
push-join: 5.47314453125 ms
concatenate: 4.9619140625 ms
strings 100000
join-only: 7.6240234375 ms
push-join: 57.37109375 ms
concatenate: 67.028076171875 ms
strings 1000000
join-only: 67.666259765625 ms
push-join: 319.3837890625 ms
concatenate: 609.8369140625 ms
strings 10000000
join-only: 824.260009765625 ms
push-join: 3207.129150390625 ms
concatenate: 5959.56689453125 ms

火狐:

strings 10
join-only: 0ms
push-join: 1ms
concatenate: 0ms
strings 100
join-only: 0ms
push-join: 0ms
concatenate: 0ms
strings 1000
join-only: 0ms
push-join: 1ms
concatenate: 0ms
strings 10000
join-only: 1ms
push-join: 2ms
concatenate: 0ms
strings 100000
join-only: 5ms
push-join: 11ms
concatenate: 8ms
strings 1000000
join-only: 39ms
push-join: 88ms
concatenate: 98ms
strings 10000000
join-only: 612ms
push-join: 1095ms
concatenate: 3249ms

要测试的代码:

for (var n = 10; n <= 10000000; n*=10) {
    
    var iterations = n;

    console.log("strings", iterations);
    console.time("push-join");
    arr = [];
    for (var i = 0; i< iterations; i++) {
        arr.push("a b c d e f g h i j k l m");
    }
    console.time("join-only");
    content = arr.join(",");
    console.timeEnd("join-only");
    console.timeEnd("push-join");

    content = "";

    console.time("concatenate");    
    for (var i = 0; i< iterations; i++) {
        content += "a b c d e f g h i j k l m";
    }
    console.timeEnd("concatenate");

}

答案 6 :(得分:1)

2021 年测试

见下面的代码。结果:

Firefox:在常规使用中,push+join 比字符串 concat 慢 80%

Chrome:在常规使用中,push+join 比字符串 concat 慢 140%

function test(items = 100, rep = 1000000) {
  let str

  console.time('concat')
  for (let r = 0; r < rep; r++) {
    str = ''
    for (let i = 0; i < items; i++) {
      str += i
    }
  }
  console.timeEnd('concat')

  console.time('push+join')
  for (let r = 0; r < rep; r++) {
    const arr = []
    for (let i = 0; i < items; i++) {
      arr.push(i)
    }
    str = arr.join('')
  }
  console.timeEnd('push+join')
}

答案 7 :(得分:0)

这取决于:

铬79.0.3945

  

阵列连接速度降低30%

Firefox 71.0.0

  

String Concat的速度降低90%

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

答案 8 :(得分:-1)

展开运算符,用三个连续点(...)编写,是ES6中的新功能,使您能够将可迭代对象扩展或传播到多元素。

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

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

相关问题