js合并字符串的3种⽅法和效率/* abc abc abc*/
function concat(s,n)
{
let a = new Array(n);
a.fill(s);
let str = a.join('');
a = null;
return str;
}
function concat1(s, n){
let count = n;
let str = '';
while(count > 0)
{
str += s;
count --;
}
return str;
}
function tco(f) {
var value;
var active = false;
var accumulated = [];
return function accumulator() {
accumulated.push(arguments);
if(!active) {
active = true;
while(accumulated.length) {
value = f.apply(this, accumulated.shift());
}
active = false;
return value;
}
};
}
const concat2 = tco(function ( s,n, ret = '')
{
if(n == 1) return s+ret;
return concat2(s, n-1, ret+s);
})
let n = 10000*100;
let startTime = new Date();
let str = concat('abc',n);
let endTime = new Date();
字符串截取方法js
let str1 = concat1('abc',n);
let endTime1 = new Date();
let str2 = concat2('abc',n);
//console.log('the result is \"' + str2 + '\"');
let endTime2 = new Date();
//console.log('the result is \"' + str + '\"');
console.log('time:' + (Time() - Time()));
console.log('time1:' + (Time() - Time()));
console.log('time2:' + (Time() - Time()));
1. 10000*100
time:135
time1:364
time2:684
2:10000*1000
time:1357
time1:7395
time2:6883
time:1357time1:7395time2:6883
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论