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

210
Vistas
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 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