recently I had problem with long minor and major gc sweeps in my app, overall I was allocating too much memory too quickly. But it also got me thinking on such case: Often when there are multiple arguments to function I'm creating an object to not mess up order and I'm wondering how much does it affect minor and major gc sweeps. Consider 2 scenarios:
const fn = (arg1, arg2, arg3, arg4) => {...}
const fn = ({ arg1, arg2, arg3, arg4 }) => {...}
Subtle change, but in first example we are not allocating any memory right? All of the variables are already defined and have memory allocated for them. In the second example if we want to call this function, we have to create object therefore allocate memory.
So I was wondering if I'm calling 2nd function very quickly, like every few ms, is it making garbage collector sweeps a lot longer, or maybe it's negligible? Does it even allocate memory or JS engines are taking care of that and it's basically free as in 1st example?
(V8 developer here.)
in first example we are not allocating any memory right?
Right.
if I'm calling 2nd function very quickly, like every few ms, is it making garbage collector sweeps a lot longer, or maybe it's negligible?
Should be negligible: an object every few milliseconds is no problem for the garbage collector. In particular, these objects should be very short-lived (unless you're storing references to them somewhere), so they should never make it to the old generation, and hence won't affect major GC cycles.
If you wanted to execute this call hundreds of times per millisecond, there might be more reason to have performance concerns, but see the point below.
Does it even allocate memory or JS engines are taking care of that and it's basically free as in 1st example?
Depends.
Generally speaking, you're creating an object there, and that's an allocation.
However, if that call is very hot, then the calling function will get optimized eventually, and the callee might get inlined, and after inlining the parameters object might get optimized out (by a technique often called "escape analysis" in the optimizing compiler). In that case, the result will be as efficient as the first example.