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

79
Visualizações
Counting Matches using Asynchronous Execution

Below, I have written a function that takes an array and a key to search and count the number of times the key matches an element in an array (in parallel). I am attempting to modify my function to count the number of matches asynchronously but don't know how to best go about it. Any insight or examples would be greatly appreciated.

My Code:

function countMatches(arr, key, done) 
{
    const threads = 4;
    const pool = new StaticPool({
        size: threads,
        task: function (a) 
        {
            let m = 0;
            for (let i = 0; i < a.length; i++) {
                if (a[i] == this.workerData) m++;
            }
            return m;
        },
        workerData: key
    });

    const size = arr.length / threads;

    let res = 0, finished = 0;
    for (let i = 0; i < threads; i++) 
    {
        (async () => 
        {
            let r = await pool.exec(arr.slice(i * size, (i + 1) * size));
            console.log("Result: " + r);
            res += r;
            finished++;
            if (finished == threads) 
            {
                done(res);
                pool.destroy();
            }
        })();
    }
}
about 4 years ago · Santiago Gelvez
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