string's replace,use RegExp's group or native string's method
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
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 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] : '';
})
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
mix, use string's replace+RegExp
|
|
pending… |
mix1,also regexp but no group,and slice string in the function
|
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
0 Comments