replace or forEach or for..of

Benchmark created on


Setup

const template = `Olá {{1}} 😄 
Esse é um teste utilizando modelos com imagens e botões
Enviado pelo {{2}}`;

const variables = [
  {
    "type": "text",
    "text": "Daniel"
  },
  {
    "type": "text",
    "text": "Atendimento Boti"
  }
]

Test runner

Ready to run.

Testing in
TestOps/sec
replace
function mergeParamsWithTemplateReplace(templatePlaceholder, variables, kind) {
    return templatePlaceholder.replace(/\{\{(\d)\}\}/gm, (_match, p1) => {
      return `${variables[p1 - 1][kind]}`;
    });
  };
    mergeParamsWithTemplateReplace(template, variables, "text");
ready
forEach
const mergeParamsWithTemplateForEach = (templatePlaceholder, variables, kind) => {
  	let olar;
variables.forEach((param, index) => {
      olar = templatePlaceholder.replace(`{{${index + 1}}}`, param[kind]);
    });
    return olar;
  };

mergeParamsWithTemplateForEach(template, variables, "text");
ready
for of
for (const [index, value] of ["a", "b", "c", "d", "e"].entries()) {
  console.log(index, value);
}

const mergeParamsWithTemplateForOf = (templatePlaceholder, variables, kind) => {
  	let olar;
  	for (const [index, param] of variables.entries()) {
      olar = templatePlaceholder.replace(`{{${index + 1}}}`, param[kind]);
  	}
    return olar;
  };

mergeParamsWithTemplateForOf(template, variables, "text");
ready

Revisions

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