Array Join vs String Concat

aja*_*221 63 javascript arrays performance connection-string 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("");
Run Code Online (Sandbox Code Playgroud)

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];
}
Run Code Online (Sandbox Code Playgroud)

Ali*_*guy 42

ECMAScript中的字符串连接速度更快.这是我创建的基准,向您展示:

http://jsben.ch/#/OJ3vo

  • @ ajax333221:*"我们在连接示例中不必要地重新创建数组"*创建数组以加入它**是重点**.您更新的jsperf将苹果与橙子进行比较.如果你*已经*有一个数组,是的,`join`会更快.但人们正在比较的是*创建一个数组只是为了加入它,而不是做字符串连接.完全不同的水壶鱼. (7认同)
  • 苹果到苹果:连接不同长度的字符串2000次,要么在数组中构建并执行`join`,要么使用字符串连接.http://jsperf.com/yet-another-array-vs-concat Concat凭借现代引擎获得性能,当然也因可读性/可维护性/可调试性而赢得胜利.当我在10年前尝试这个时,数组的性能略好一些.不再那样了. (3认同)

Rya*_*rty 8

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

  • 最新的测试支持这个:https://github.com/nodejs/node/issues/6610 (2认同)

vit*_*y-t 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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 我遇到了这个话题,其他答案可能在2011年正确,但此时加入确实更好. (2认同)

K V*_*Vij 7

join当字符串数组已经存在时,速度要快得多。真正的比较是比较:

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

对于少量迭代和字符串,使用推送连接或连接并不重要。然而,对于大量字符串,数组推送和连接在 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
Run Code Online (Sandbox Code Playgroud)

火狐浏览器:

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
Run Code Online (Sandbox Code Playgroud)

要测试的代码:

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");

}
Run Code Online (Sandbox Code Playgroud)


Tob*_*bia 7

2021年测试

请参阅下面的代码。结果:

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

Chrome:在常规使用中,push+join 比 string 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')
}
Run Code Online (Sandbox Code Playgroud)

  • +1更新答案,但实际上,当我回顾10年时,我感到畏缩,我担心性能,这都是关于代码维护的 (2认同)