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

225
Visualizações
Is "for(key in args)" the fastest way to consume an args object?

I was benchmarking a potential micro-optimization which might help speed up one of my tightest loops, and got some interesting results:

https://jsbench.me/hyl3rhq5hx/2

The test was checking two different methods of pulling properties out of an args object:

// simply using "if" to check if args have certain properties
function checkIfIn(args) {
    if (args) {
        if (args.a) { out += args.a; } 
        if (args.b) { out += args.b; } 
        if (args.c) { out += args.c; } 
        if (args.d) { out += args.d; } 
        if (args.e) { out += args.e; } 
        if (args.f) { out += args.f; } 
        if (args.g) { out += args.g; } 
        if (args.h) { out += args.h; } 
        if (args.i) { out += args.i; } 
        if (args.j) { out += args.j; } 
        if (args.k) { out += args.k; } 
        if (args.l) { out += args.l; } 
        if (args.m) { out += args.m; } 
        if (args.n) { out += args.n; } 
        if (args.o) { out += args.o; } 
        if (args.p) { out += args.p; } 
    }
}


---------- VS --------------------

// iterating over args keys and passing to function
const fnMap = {
    a: (args) => out += args.a,
    b: (args) => out += args.b,
    c: (args) => out += args.c,
    d: (args) => out += args.d,
    e: (args) => out += args.e,
    f: (args) => out += args.f,
    g: (args) => out += args.g,
    h: (args) => out += args.h, 
    i: (args) => out += args.i, 
    j: (args) => out += args.j, 
    k: (args) => out += args.k, 
    l: (args) => out += args.l, 
    m: (args) => out += args.m, 
    n: (args) => out += args.n, 
    o: (args) => out += args.o, 
    p: (args) => out += args.p,     
}
function iterateKeysIn(args) {
    if (args) {
        for (const key in args) {
            const fn = fnMap[key];
            fn && fn(args);
        }
    }
}

For small amounts of arg props (~4), the two benchmarks yield nearly equal results. But for each additional arg, the iterateKeysIn method becomes progressively more performant (in my case, I have ~20 args which coincidentally yields about a ~20% speed boost).

This result simultaneously makes sense and surprises me. On one hand, not checking for properties that aren't there is efficient. But on the other, getting an iterator for keys, looking up properties in an object, and then passing values onto another arrow function feels overly complex.

In particular, I don't see how these two can be so close to each-other's performance at low numbers of props.

Is this actually the fastest way to consume an args object in Javascript?

about 4 years ago · Juan Pablo Isaza
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