dynamic templating (v2)

Revision 2 of this benchmark created on


Setup

const fnTemplate = (template, parameters) => {
  const names = Object.keys(parameters);
  const values = Object.values(parameters);
  return new Function(...names, `return \`${template}\`;`)(...values);
};

const regexTemplate = (template, parameters) => {
  return Object.entries(parameters).reduce(
    (result, [arg, val]) => result.replace(`$\{${arg}}`, `${val}`),
    template
  );
};

Test runner

Ready to run.

Testing in
TestOps/sec
function
fnTemplate("my name is ${name} and i am ${age} years old", {name: "rodr", age: 22})
ready
regex
regexTemplate("my name is ${name} and i am ${age} years old", {name: "rodr", age: 22})
ready

Revisions

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