string's replace,use RegExp's group or native string's method

JavaScript performance comparison

Test case created by 十年灯

Preparation code

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] : '';


Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
mix, use string's replace+RegExp
mix(aString, {
  name: 'LIX',
  age: 11
mix1,also regexp but no group,and slice string in the function
mix1(aString, {
  name: 'LIX',
  age: 11

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers