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

161
Visualizações
Is there a faster way to loop through nested array of objects than nested for loops?

I've been learning the fundamentals of Big(o) and after looking at some old code I've written, I habitually use nested for loops for problems like this instead of implementing a better approach with a faster run time. For instance:

const webhooks = [
  [
    {
      topic: "CUSTOMERS_CREATE",
      path: `${process.env.HOST}/webhooks/customers/create`,
    },
    {
      topic: "CUSTOMERS_UPDATE",
      path: `${process.env.HOST}/webhooks/customers/update`,
    },
    {
      topic: "CUSTOMERS_DELETE",
      path: `${process.env.HOST}/webhooks/customers/delete`,
    },
  ],
  [
    {
      topic: "CHECKOUTS_CREATE",
      path: `${process.env.HOST}/webhooks/checkouts/create`,
    },
    {
      topic: "CHECKOUTS_UPDATE",
      path: `${process.env.HOST}/webhooks/checkouts/update`,
    },
  ],
  [
    {
      topic: "ORDERS_CREATE",
      path: `${process.env.HOST}/webhooks/orders/create`,
    },
    {
      topic: "ORDERS_UPDATED",
      path: `${process.env.HOST}/webhooks/orders/update`,
    },
    {
      topic: "ORDERS_DELETE",
      path: `${process.env.HOST}/webhooks/orders/delete`,
    },
  ],
  [
    {
      topic: "PRODUCTS_CREATE",
      path: `${process.env.HOST}/webhooks/products/create`,
    },
    {
      topic: "PRODUCTS_UPDATE",
      path: `${process.env.HOST}/webhooks/products/update`,
    },
    {
      topic: "PRODUCTS_DELETE",
      path: `${process.env.HOST}/webhooks/products/delete`,
    },
  ],
];

 // Registering the webhooks
 webhooks.forEach(async (elm) => {
  elm.forEach(async (el) => {
    Shopify.Webhooks.Registry.addHandler(el.topic, {
      path: el.path,
      webhookHandler: webhookController,
    });
  });
});

In this snippet, I am looping over the initial array, then the nested array to finally get the topic and path for each element. Then I make a function call with those properties for further processing. While a nested for loop works, I know it will have a runtime of O(n^2) which is awful at scale. I thought about converting this array of objects into a map, but are there caveats to this approach I'm missing? If so, what alternative methods could I use to achieve a much more ideal runtime like O(log(n))?

about 4 years ago · Santiago Trujillo
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