#question about the performance of Object.keys and Object.value when filtering with key and without

22 messages · Page 1 of 1 (latest)

gray flare
#

why does using a filter with a key for acess Element in a enum make it slower than using Object.values()? i thought acessing values by key would be faster

code (this code was code Typescript transpiled).

  
//test 1
var Partials;
(function (Partials) {
    Partials[Partials["User"] = 0] = "User";
    Partials[Partials["Channel"] = 1] = "Channel";
    Partials[Partials["GuildMember"] = 2] = "GuildMember";
    Partials[Partials["Message"] = 3] = "Message";
    Partials[Partials["Reaction"] = 4] = "Reaction";
    Partials[Partials["GuildScheduledEvent"] = 5] = "GuildScheduledEvent";
    Partials[Partials["ThreadMember"] = 6] = "ThreadMember";
})(Partials || (Partials = {}));


Object.values(Partials).filter(e => typeof e !== 'string')

//test 2
var Partials;
(function (Partials) {
    Partials[Partials["User"] = 0] = "User";
    Partials[Partials["Channel"] = 1] = "Channel";
    Partials[Partials["GuildMember"] = 2] = "GuildMember";
    Partials[Partials["Message"] = 3] = "Message";
    Partials[Partials["Reaction"] = 4] = "Reaction";
    Partials[Partials["GuildScheduledEvent"] = 5] = "GuildScheduledEvent";
    Partials[Partials["ThreadMember"] = 6] = "ThreadMember";
})(Partials || (Partials = {}));
Object.keys(Partials).filter(key => typeof Partials[key] === 'string')

i think using keys might be causing more recursion. For exemple, when i use Object.value(), i already receive the value in the param, so i just need to check type. but when i use keys, i have to search in array again using key, and check type. is that why Object.values() is faster in my code? i dont know, that's just what i think...

pseudo raven
#

How sure are you that neither of those is getting JIT compiled into effectively a no-op

#

the problem with microbenchmarks is that the runtime is very smart about not doing unnecessary work and it's very hard to get an effective measurement because of that

carmine crystal
#

i think using keys might be causing more recursion.
there's no recursion here

gray flare
carmine crystal
#

they have negligibly similar performance

#

Partials[key] is technically more work due to the access, and Object.values and Object.keys might be doing different amounts of work under the hood, but really it's just negligible

#

it might start becoming relevant if you have, say, an object with a million keys

#

but at the scale you're testing there's not going to be any tangible difference

carmine crystal
gray flare
carmine crystal
pseudo raven
#

If you're not profiling an actual production system there are very very good odds that the performance characteristics of what you're evaluating are not realistic

carmine crystal
pseudo raven
rough fiber
#

js should have a way to toggle both the interpreter and compiler

carmine crystal
#

toggle as in... turning them off/on? that would just make it not run

rough fiber
#

toggle as in jit || compile || interpret

#

as in jit: normal, compile: agressive optimisations with less strict deoptimizer and interpret: no compiler

carmine crystal
#

those aren't separate steps

#

well, interpretation is, i guess

#

"jit" is the type of compilation: just-in-time compilation, as opposed to ahead-of-time compilation
iirc, node does support aot compilation? it's just very uncommon
either way, it's compiled to bytecode, which is then interpreted by the VM into machine code