6 ms·
Not an expert in JS, but maybe this makes sense in JS where everything is passed as objects anyway and having a single param is often more readable there. But
by hwpythonner 1y ago
Not an expert in JS, but maybe this makes sense in JS where everything is passed as objects anyway and having a single param is often more readable there.
But in native languages like C/C++/etc, this pattern usually hurts performance. Separate arguments go into registers (faster but depends on number of arguments), while structs often involve indirection, and sometimes alignment/padding issues. Also, passing a const struct doesn’t guarantee the fields inside are truly const.
That said, it's context dependent. If you're passing stuff through multiple layers or want a more stable function signature, using a struct can be cleaner. Just not something I’d generalize to any language or any use case.
- taeric 1y agoI think the dream has long been that a compiler could optimize away any inefficiencies that can be mechanically described. As such, if there is a benefit to seeing the same structure in many places, using a struct would be ideal. I don't think we are near said dream, yet.
- deleted 1y ago[deleted]
- dicytea 1y agoDoesn't seem to be a problem, at least for Rust: https://godbolt.org/z/fToxz3d7a https://godbolt.org/z/fToxz3d7a. Functions with plain arguments and a struct both produce identical assembly output.
- carlos-menezes 1y agoC++ for reference: https://godbolt.org/z/7G6qxK6T8 https://godbolt.org/z/7G6qxK6T8
- hwpythonner 1y agoTry compiling with optimizations. I think by default this site doesn't add optimization flags. Here what happens with optimizations: https://godbolt.org/z/G18zd7chP https://godbolt.org/z/G18zd7chP Look at the registers usages vs stack
- carlos-menezes 1y agoGot it! Thanks for the link.
- physicsguy 1y agoIf you add a third argument you get different assembly
- airstrike 1y agoAlso worth noting clippy will warn about writing functions with too many arguments for the same reason listed in TFA https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments https://rust-lang.github.io/rust-clippy/master/index.html#to...
- the_other 1y agoI’ve seen some evidence it hurts performance in JS, too. If you’re doing something “a lot”, like media qstreaming, or realtime updates, ordered params are likely faster. I agree with others that the destructuring is a nice syntax to read and write. Yet, I have started to feel it’s an overused pattern, deployed by habit rather than consideration (in my code anyway). I can’t put my finger on why. It’s possibly a gut-feel that my functions would be better taking feeer arguments.
- oneoverten 1y agoYes. This is true for JS too, object-wrapping parameters just to make them named will add runtime overhead.
- fenomas 1y agoNot in current chrome, at least for simple cases (like a short function with a single argument). It benchmarks the same with or without the param wrapper.