Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
Adding Text and Value to Array from CreateElement

Adding createElements - text and Value without the necessary forLoop to the Array.

Tried map, forEach, but memory wise...it is still lagging with numerous entries.

Please help.

templateList example:  ["a", "b", "c", "d", "e", "f"];

var templateList = new Array();
var selection = document.getElementsByName("name")[0];

for(var i = 0; i < templateList.length; i++) {
var open = document.createElement("Option");
open.text = templateList[i];
open.value = templateList[i];
selection.add(open);
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you might get a little better performance using a DocumentFragment to build your options and render them all at once into the dom. I´d say there is no non-iterative way

Because all of the nodes are inserted into the document at once, only one reflow and render is triggered instead of potentially one for each node inserted if they were inserted separately.

var templateList =  ["a", "b", "c", "d", "e", "f"];
var selection = document.getElementsByName("name")[0];
var opts = new DocumentFragment();

for(var i = 0; i < templateList.length; i++) {
  var open = document.createElement("Option");
  open.text = templateList[i];
  open.value = templateList[i];
  opts.appendChild(open);
}
selection.appendChild(opts);
<select name="name">


</select>

I tested with js-bench and got only minimal advantage (+- 5%) though theoretically it should be less "lagging"

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create all option string and then add it to innerHTML of selection. This will be very efficient compared to your solution.

const templateList =  ["a", "b", "c", "d", "e", "f"],
      selection = document.getElementsByName("name")[0],
      options = templateList.map(v => ` <option value="${v}">${v}</option>`).join('');
selection.innerHTML = options;
<select name="name"></select>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda