数组连接vs字符串连接
Array Join vs 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=myarray.join("");
字符串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];
}
ECMAScript中的字符串连接更快。下面是我创建的一个基准:
http://jsben.ch//OJ3vo
从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
。
我可以肯定地说使用Array.join()
更快。我编写了几段JavaScript代码,通过删除字符串操作而使用数组,显著提高了性能。
当字符串数组已经存在时,join
更快。真正的比较应该是:
- 将元素压入数组,然后将它们连接起来以构建字符串
- 每次连接字符串而不使用数组
对于少量迭代和字符串,使用push-and-join还是concatate都无关紧要。然而,对于大量字符串,数组push和join似乎在chrome和firefox中都更快。
下面是1000万到1000万个字符串的代码和测试结果:
Chrome: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
Firefox: 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");
}
2021 test
见下面的代码。结果:
Firefox: push+join 在常规使用中比string concat慢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')
}
这取决于:
铬79.0.3945
Firefox 71.0.0Array Join慢30%
https://jsperf.com/lin-array-join-vs-string-concatString Concat慢90%
根据这个谷歌文档标题为'优化JavaScript代码'字符串concat比数组连接慢,但显然这不是真正的现代JavaScript引擎。
我为他们在文档中使用的斐波那契测试示例做了一个基准测试,它显示连接(粘合)字符串几乎是使用Array
join
的4倍。
对于固定长度的数字数组,手动连接更快。
下面是测试这两个操作的JSPerf测试: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%。
我认为这不仅是性能问题,还有内存问题
作为字符串是不可变的,这意味着每次你连接一个字符串,在你的内存中创建一个额外的字符串
但是数组是可变的,这意味着它将保持相同的内存地址
当然这也取决于语言,一般来说,数组是一个更好的解决方案
展开运算符,用三个连续的点(…)是ES6中新增的,它使您能够扩展或扩展可迭代对象到多个元素中。
const books = ["Don Quixote", "The Hobbit", "Alice in Wonderland", "Tale of Two Cities"];
console.log(...books);
印刷品:《堂吉诃德》《霍比特人》《爱丽丝梦游仙境》《双城记》
- 对级联下拉列表使用不同的连接字符串
- 连接字符串时如何留出空格
- 用javascript中的三元运算符连接字符串
- 如何在JavaScript中移除在循环中连接字符串时添加的额外字符
- 在 JavaScript 中连接字符串或数组是否更快
- 我的 base64 连接字符串有 = 字符.如何摆脱它们
- 当字符串为空时,以不同的方式连接字符串
- 这将如何在JavaScript中完成.在连接字符串中使用变量
- 使用JavaScript连接字符串有什么问题
- 使用 JavaScript 警报框在 php 中连接字符串
- 对最后一个条目进行特殊处理的连接字符串的最简单方法
- 连接字符串以打开带有 控件的窗口弹出窗口
- 在 JavaScript / jQuery 中连接字符串
- 连接字符串时是否有用于间距的 JSHint 设置
- 在 Javascript 中连接字符串
- 在 HTML 中连接字符串
- 在名称属性 Struts 中连接字符串
- jQuery 函数在连接字符串后返回 [对象对象]
- 用于屏蔽连接字符串密码的正则表达式
- NodeJS MongoDB 连接字符串问题