I strongly disagree with point 1. It looks way too much like optimising for code size rather than clarity, and it's completely pointless. People who care about saving 7 bytes are going to be shocked at what happens when they run their code through a minimiser, which is what's going to happen during the build, and then those people are going to be shocked at what happens when their script is gzipped and maybe even SPDYd up during transmission over https.
In point 8, I think you should default to using const for the variable. It might seem like it's being re-assigned in the for clause, but it's actually scoped to the enclosed block so defining it as const is usually the safe move.
From my experience, the usage of !! is much more common than the explicit Boolean(...) cast. Actually, I have never even seen it in practice yet. So from my POV, !! feels familiar and straightforward while the cast feels ...hmm ...very exotic.
Really? I use explicit casts all the time as they seem so much clearer in intent. The explicit meaning of !! is "not-not", rather than "convert to boolean". Similarly for Number (or parseInt, parseFloat, etc. depending on use case) vs +... I want to convert to a number, not make the variable positive.
Totally agree — shorter isn't always better, and we shouldn't optimize for code brevity but for code clarity. I've collected many more similar examples, though !! is probably the most harmless and commonly used of all.
#1 If you don't use terser or another JS minification engine, go ahead, otherwise it makes no difference whatsoever.
#5/6/7 Be aware that proxies and getters will only be called once in destructuring and spreading. If those are used e.g. to provide subscription for reactive behavior (e.g. in Solid.js), this will break your code.
#8 never use forEach like this to copy an array. Use const array = [...items] for a shallow copy to be even faster.
This type of "optimization" is overestimated in most cases. Javascript does a very good job optimizing execution (with some very special exceptions like for/in), and the effort to save some bytes often does not really pay back. On the long run, good readability and a clear structure shouldn't be sacrificed just to minimize the code size.
I strongly disagree with point 1. It looks way too much like optimising for code size rather than clarity, and it's completely pointless. People who care about saving 7 bytes are going to be shocked at what happens when they run their code through a minimiser, which is what's going to happen during the build, and then those people are going to be shocked at what happens when their script is gzipped and maybe even SPDYd up during transmission over https.
In point 8, I think you should default to using
constfor the variable. It might seem like it's being re-assigned in theforclause, but it's actually scoped to the enclosed block so defining it asconstis usually the safe move.From my experience, the usage of
!!is much more common than the explicitBoolean(...)cast. Actually, I have never even seen it in practice yet. So from my POV,!!feels familiar and straightforward while the cast feels ...hmm ...very exotic.Really? I use explicit casts all the time as they seem so much clearer in intent. The explicit meaning of
!!is "not-not", rather than "convert to boolean". Similarly forNumber(orparseInt,parseFloat, etc. depending on use case) vs+... I want to convert to a number, not make the variable positive.I have found multiple bugs in react applications because of that !!
Totally agree — shorter isn't always better, and we shouldn't optimize for code brevity but for code clarity. I've collected many more similar examples, though
!!is probably the most harmless and commonly used of all.Yeah, Airbnb style guide is trash. It also recommends
const fn = function longAssDescriptiveName () {}overfunction normalFunctionName() {}🤢#1 If you don't use terser or another JS minification engine, go ahead, otherwise it makes no difference whatsoever.
#5/6/7 Be aware that proxies and getters will only be called once in destructuring and spreading. If those are used e.g. to provide subscription for reactive behavior (e.g. in Solid.js), this will break your code.
#8 never use forEach like this to copy an array. Use
const array = [...items]for a shallow copy to be even faster.Otherwise, avoid premature optimisations.
Why age from example 1 is a boolean?
Despite its conciseness I'm not a big fan of the "!!". It's not very explicit about what it does.
Does "less is more" mantra is that relevant in programmation? Do we have to write minimal code no matter the situation? I don't think so.
This type of "optimization" is overestimated in most cases. Javascript does a very good job optimizing execution (with some very special exceptions like for/in), and the effort to save some bytes often does not really pay back. On the long run, good readability and a clear structure shouldn't be sacrificed just to minimize the code size.
About #1,
!!it is just double logical operator, it exists from first version of JS, not related to ES6 at allThank you
Very informative. Thank you for the post.
informative content. Good work!
I'm not sure what "minimal" is supposed to mean here?
Trying to get better with JS anyone have any tips or recommendations