jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
测试拼接复杂的字符串的易用且效率不错的方法。
Test the way to concat a complex HTML string, fast and easy to use.
var aString = 'My name is ${name},I\'m ${age}.My name is ${name},I\'m ${age}.My name is ${name},I\'m ${age}.My name is ${name},I\'m ${age}.My name is ${name},I\'m ${age}.My name is ${name},I\'m ${age}.'
function nativeJoin(group) {
return 'My name is ' + group.name + ',I\'m ' + group.age + '.My name is ' + group.name + ',I\'m ' + group.age + '.My name is ' + group.name + ',I\'m ' + group.age + '.My name is ' + group.name + ',I\'m ' + group.age + '.My name is ' + group.name + ',I\'m ' + group.age + '.My name is ' + group.name + ',I\'m ' + group.age + '.'
}
function mix(str, group) {
return str.replace(/\$\{([^{}]+)\}/gm, function(m, n) {
return (group[n] != undefined) ? group[n] : '';
});
}
function mix1(str, group) {
return str.replace(/\$\{[^{}]+\}/gm, function(m, n) {
n = m.slice(2, -1);
return (group[n] != void 1) ? group[n] : '';
})
}
function loopMix0(str, group) {
var reg = /\$\{([^{}]+)\}/gm,
res;
while (res = reg.exec(str)) {
str = str.substr(0, res.index) + ((group[res[1]] != undefined) ? group[res[1]] : '') + str.substr(res.index + res[0].length);
}
return str;
}
function loopMix1(str, group) {
var reg = /\$\{([^{}]+)\}/gm,
res, returnString = '',
start = 0;
while (res = reg.exec(str)) {
returnString += str.substring(start, res.index) + ((group[res[1]] != undefined) ? group[res[1]] : '');
start = res.index + res[0].length;
}
returnString += str.substr(start);
return returnString;
}
function loopMix2(str, group) {
var reg = /\$\{([^{}]+)\}/gm,
res, returnString = '',
start = 0;
while (res = reg.exec(str)) {
returnString += str.substring(0, res.index) + ((group[res[1]] != undefined) ? group[res[1]] : '');
start = res.index + res[0].length;
str = str.substr(start);
// console.log(str)
reg.lastIndex = 0;
}
returnString += str;
return returnString;
}
function loopMix2_1(str, group) {
var reg = /\$\{[^{}]+\}/gm,
res, returnString = '',
start = 0,
n;
while (res = reg.exec(str)) {
n = res[0].slice(2, -1);
returnString += str.substring(0, res.index) + ((group[n] != undefined) ? group[n] : '');
start = res.index + res[0].length;
str = str.substr(start);
// console.log(str)
reg.lastIndex = 0;
}
returnString += str;
return returnString;
}
function loopMix3(str, group) {
var index = 0,
close = 0,
returnString = '';
while ((index = str.indexOf('${', index)) !== -1) {
returnString += str.substring(close, index);
close = str.indexOf('}', index);
var name = str.substring(index + 2, close);
returnString += (group[name] != undefined) ? group[name] : ''
index = close;
close += 1;
}
returnString += str.substr(close);
return returnString;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
mix, use string's replace+RegExp |
| ready |
loopMix0,loop regexp 'exec' |
| ready |
loopMix1 |
| ready |
loopMix2 |
| ready |
loopMix3,string's indexOf,no RegExp! |
| ready |
mix1,also replace+regexp,but no regexp-group,substituted with 'slice' |
| ready |
loopMix2_1,just loopMix2 but with no regexp's () |
| ready |
nativeJoin,native ++ |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.