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

78
Visualizações
JavaScript solution slower when dataset get larger

I have a dataset as below,

// e.g.
[{
  id: 'M1',
  description: 'Lorem description',
  fields: [{
    name: 'field_1',
    value: 'Lorem value 1'
  }]
}]

which I need to transform into,

[
  {
    id: 'M1',
    description: 'Lorem description',
    field_1: 'Lorem value 1'
  }
]

I wrote the code below to accomplish this. And it works well, but I don't think this is the best way to do it. How can I make my solution better performing? Because this is slower when the dataset gets larger.

const _sampleData = [{
    id: 'M1',
    description: 'Lorem description',
    fields: [{
      name: 'field_1',
      value: 'Lorem value 1'
    }]
  },
  {
    id: 'M2',
    description: 'Lorem description',
    fields: [{
        name: 'field_1',
        value: 'Lorem value 1'
      },
      {
        name: 'field_2',
        value: 'Lorem value 2'
      }
    ]
  }
];

function toObject(fields) {
  const out = {};
  for (const field of fields) {
    out[field.name] = field.value;
  }
  return out;
}

function getFlatSampleData() {
  const data = [];

  for (const item of _sampleData) {
    let out = {};
    for (const key in item) {
      if (Array.isArray(item[key])) {
        out = {
          ...out,
          ...toObject(item[key])
        };
      } else {
        out[key] = item[key];
      }
    }
    data.push(out);
  }

  return data;
}

console.log(getFlatSampleData());

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The part that seems to be the culprit is as follows:

        out = {
          ...out,
          ...toObject(item[key])
        };

Instead, since you want to flatten your object, you could do something like this:

let obj = [{
  id: 'M1',
  description: 'Lorem description',
  fields: [{
    name: 'field_1',
    value: 'Lorem value 1'
  }]
}];
let output = {};
let itemQueue = [obj];
let limit = 0;
while (limit < itemQueue.length) {
    for (let key in itemQueue[limit]) {
        if ((Array.isArray(itemQueue[limit][key])) || (typeof itemQueue[limit][key] === "object")) {
            if ((typeof itemQueue[limit][key].name !== "undefined") && (typeof itemQueue[limit][key].value !== "undefined")) {
                output[itemQueue[limit][key].name] = itemQueue[limit][key].value;
            } else {
                itemQueue.push(itemQueue[limit][key])
            }
        } else {
            output[key] = itemQueue[limit][key];
        }
    }
    limit++;
}
console.log(output);

The idea is to not generate new objects always, but rather use a combination of a stack and a loop.

about 4 years ago · Juan Pablo Isaza Relatório

0

The required processing time is expected to increase as data sets get larger. The code only accesses each element once and the transformation could not happen in less accesses. So from a high level computational perspective it's complexity is ok. Apart from the above code improvements I could propose the following two things.

  1. Preallocate the memory needed for the objects. Instead of calling push every time.

    function getFlatSampleData() { const data = new Array(_sampleData.length); for (const item of _sampleData) { let out = {}; for (const key in item) { if (Array.isArray(item[key])) { out = { ...out, ...toObject(item[key]) }; } else { out[key] = item[key]; } } data.push(out); } return data; }

  2. Use workers or a parallel JavaScript Framework to parallelize the process.

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