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中的字符串连接速度更快.这是我创建的基准,向您展示:
我可以肯定地说使用Array.join()更快.我已经研究了一些JavaScript代码,并通过删除字符串操作而有利于数组来大大加快了性能.
从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.
join当字符串数组已经存在时,速度要快得多。真正的比较是比较:
对于少量迭代和字符串,使用推送连接或连接并不重要。然而,对于大量字符串,数组推送和连接在 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)
请参阅下面的代码。结果:
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)
| 归档时间: |
|
| 查看次数: |
76809 次 |
| 最近记录: |