JavaScript is no doubt one of the coolest languages in the world and is gaining more and more popularity day by day. So the developer community has...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
Great, Garvit! It's always great to know the language so well and bring about these unconventional ways to solve problems. However, it's always recommended to write code that is self-explanatory. And, I find majority of these tricks to be confusing (to an average eye). Use them when you really have to and leave a comment to make your intention clear.
On that note β which I completely agree with β it's interesting how readable this is
Array.from(dogs)
compared todogs.map()
if map wasn't so widely adopted :)On that note, I'd love to see how performant
Array.from()
is compared to.map()
that I know doesn't perform very wellNow you've inspired a more ambitious project. Building a custom standalone performance measurement tool for Javascript.
It could be something similar to Runkit, but strictly for benchmarking the various methods and approaches to the same end goal. Like which is faster?
OR
π€π€π€
I can tell you for sure that the for loop is way more performant than map. But if you do compare everything, I'm definitely interested to read.
Noted! and thanks for reading the article!
Map an array without .map()
Pick one. You can't say one is difficult and complicated and nobody will understand it.
And the other one is surprising, rarely taught or known but way easier to understand?
Actually, I really like that method and it is way easier than
.map()
, that's why I recommended that, but noted! Thanks for reading the articlePlease explain how your method is "way easier than .map()"?
I just don't understand your thinking here, the concepts are exactly the same, the syntax is almost exactly the same (actually longer), and you're introducing a somewhat unfamiliar syntax to most to achieve something that everyone likely understands already with .map().
This title makes it seem like these are tips and tricks worthwhile to use that are not that popular but extremely useful.
Most of these tips are actually well known among JS developers and aren't ones "That No One Teaches", such as the performance tip, the short for loop, using sets to remove duplicates from arrays, and swapping values with destructuring. The only ones that aren't as known are the first couple, which don't seem to be that useful.
Sorry, if that sounds rude because that isn't my intention. I am just trying to point out that most of these tips are either well known or not useful and the title is pretty misleading then.
You're right and I like your avatarπ
Thanks! I donβt remember how I created that
Noted
Also, u can convert number to string in this way:
That is interesting and thanks for sharing!!
or
num + ''
This method described in the article :/
I believe you meant
There's another case where it is extremely handy - creating a fresh array with elements initialized from a function.
The issue with Array.prototype.fill() is that:
And Array.prototype.map() requires an already initialized array.
With Array.from() an array can be created and initialized with element content that varies by index in one fell swoop.
I kind of disagree, finding map much more readable.
Array.from({length: 4}).map((_value, index) => (index + 1) * 3)
With
it always ground my gears that to arrive at the desired array another disposable array had to be created and "filled" just to convey that I want
length
elements. Andwas clunky enough to make me want to use a
for
loop.So if I'm are already using
Array.from()
I might as well use the optionalmapFn
andthisArg
parameters.... but that's just me.
I'd say that
Array.from()
isn't the most handy as a substitute forArray.map()
. Rather, it's handy for transforming iterables that aren't already arrays.Like, to get the names of commenters on this page, try this in the dev tools console:
The NodeList returned from
document.querySelectorAll
isn't an array, but this lets you convert it to an array while performing a transformation at the same time.The problem with JavaScript is that.... itβs not taught βin contextβ, itβs mainly always a βconsole.logβ that is used for the answer, instead of truly implementing it into real examples.... itβs been tough for me to learn it
Liquid syntax error: Tag '{%' was not properly terminated with regexp: /\%\}/
Make projects to learn it! Helps a lot!!!
Ya
I'm a big fan of
!!
which gets the boolean value of (pretty much) anything:!
is the opposite so!!
becomes the boolean value!!
πThe Boolean() constructor is another great way to interpret falsey values.
Thanks for sharing! ππ
Hi, thanks for the article.
I think there is a small mistake in the "Number to string/string to number" part...
The example for converting string to number is:
let num = "4"
let stringNumber = Number(s); //doesn't that have to be Number(num);?
Welcome and Noted!!
Interesting to learn that:
let dogsNames = Array.from(dogs, ({name}) => name);
is not as complicated as:
let dogsNames = dogs.map( ({name}) => name );
BUT..
the documentation says the y parameter in
Array.from(x,y,z)
IS a mapFnSo yes, you do not type those 3 letters... but you still have to understand you are mapping an Array.
Since you mention the Performance API; this gives a very interesting result:
for performance you use dates but since you are using console.log you could use
console.time()
can you please remove the CSS tag since this post is not about CSS? thanks
Ya sorry actually I wrote it before because I wanted to create a CSS post π thanks for the reminder
G R E A T work...
There are also many other great tricks you can do to optimize code, add quality and also provide C#-like or JAVA-like paradigms to JavaScript.
Check my web framework and especially the JS extensions for more cool ideas.
github.com/g0d/micro-MVC
github.com/g0d/micro-MVC/tree/mast...
Thanks for sharing Garvit! About your first topic, Functional Inheritance, I don't actually agree with what you've said. Since the idea is to hide the object within a function, it kinda works, BUT, that's totally different from the concept of 'private' as data encapsulation. You can still get the object and do whatever you want. All that you doing there is hiding an object inside another, and then retrieving it. Also, be careful when using 'var', especially when talking about concepts like hoisting and closures. Your variables were actually globally scoped and their memory will not be immediatly freed after using that function.
Thanks For Sharing The Tip! Noted!
Nice article Garvit! I usually use the .map() substitute when creating arrays with pre-filled values. One little mistake though. array.length is a property in JavaScript not a method. It is a method in Java, C++ and others but not in JavaScript.
developer.mozilla.org/en-US/docs/W...
Thank you again for the nice article.
Another nice tip is that arrays passed to template literals get coerced to comma-separated strings like this:
const fruits = ['apples', 'oranges'];
console.log(
${fruits}
); // 'apples,oranges'Swapping variables with an array isn't going to be faster (performance-wise) than just swapping the variables with the traditional method of using a temporary variable.
stackoverflow.com/questions/162016...
One of the first comments on using a temporary array is that it is roughly two times slower than a variable swap.
Javascript processes most accesses by reference. So assigning to a temporary variable doesn't copy anything but a memory address. Creating an array, on the other hand, is almost certainly going to: Hit the memory allocation pool, add two variables to the array, update the length of the array (for no good reason), then immediately extract them back out of the array, and release the array back into the memory pool. Without even having to test that, the former is obviously faster than the latter. Swapping vars happens often in software, and usually in a loop, so writing performant code vs. one-liners is more important here.
Very nice , I have a request for you can you publish you articles on our Codeddit Programmer Community it would be a great help to everyone on Codeddit app platform.
play.google.com/store/apps/details...
Some awesome tips, thank you!
Not sure if I like overriding length with a constant value of 0 with the intention of emptying an array.
Maybe the garbage collection in JavaScript is awesome and we shouldn't worry about abandoned bits of memory, but to me this really feels like being a spoiled brat that keeps their room like a dumpster just because the family employs a maid who can clean up after them.
Really nice tips.
Question : what is the best option between using performance.now() and console.time() π€
console.time
will be limited to output in console, hence it is more suitable for temporary debugging/test situations.Any other cases I'd use
performance.now
, like displaying time in html, sending to backend etc.Good to know thanks for this tip ππ
I usually use
performance.now()
so I would recommend that butconsole.time()
is also good!!it`s ok
awesome bro, thanks for sharing
Welcome Bro!! π
Hey quick question when changing the length of the array to remove items from the array does this not cause a memory leak because of how arrays are stored, or does the garbage collector understand this change and then clean up the memory as necessary
This is an amazing job!
Perfect tricks :)
Thank you for sharing! Your post helps me improve my skills in Javascript!
Nice π
Love it thanks great tricks
thanks for this nots
Welcome bro!
This is awesome
Removing duplicates is a good trick. I'll put that one in my back pocket for later.
Yep! Thanks For Reading the articleπ
Great tips - thanks for sharing!
Welcome bro!!
Thanks for the article! Anyway some of those are just tricks that could sacrifice code readibility :)
Ya some of them! Thanks for reading tho!
Thanks for sharing Garvit
It seems a typo on Map an array without .map(), it should return dogsNames rather than friendsNames.
Noted! Thanks!
thanks for sharing Garvit!
welcome bro!
Great article. I'm particularly fond of the map without map.
Ya I use that at often nowadays
It varies from person to person like you like it but I personally find it sometimes complicated so I listed it in but thanks for the suggestion!!
For me remove duplicates in an array was interesting.....past I removed in another way. It seems like this is so easy....Thank you so much man...It was awesome ππ₯
Welcome Bro!!
I found this article super useful!
Thanks For Reading!!
Thank you Kindly, Garvit!
Greetings from Brazil!
Daaamn, this is very useful.
Array.from({length: 10}, (a,b)=> console.log(b)) instant of for loop, Thanks
Hey Garvit, how long it took to copy the content of the first paragraph from here: jstips.co/en/javascript/what-is-a-... ?