dynamic templating (v2)

Revision 2 of this benchmark created on


Setup

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

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

Test runner

Ready to run.

Testing in
TestOps/sec
Template eval
templateEval("my name is ${name} and i am ${age} years old", {name: "rodr", age: 22})
ready
Template regex
templateRegex("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.