Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda