5 Awesome JavaScript Pro Tips β that everyone should know! π«
1. Finding your code execution time
Instead of using old and hacky ways of finding how much time a particular algorithm or a code snippet is utilizing, you can use console.time() and console.timeEnd() to determine how fast your code is.
Instead of using old and hacky ways of finding how much time a particular algorithm or a code snippet is utilizing, you can use console.time() and console.timeEnd() to determine how fast your code is.
This has the added benefit that it will automatically display it in the console and will help you manage your timers in your application.
Hereβs a quick example of how you can achieve this in JavaScript.
Hereβs a quick example of how you can achieve this in JavaScript.
You have to start the timer by calling console.time() with the label and once you are done with the execution part of the code, you can always call console.timeEnd() and in the console, you are going to see the execution time.
You can do all the things which these methods do with a simple for loop as well but whenever you can use these helper methods, you should use them because they keep your code clean, concise, and declarative instead of being imperative.
3. Using async/await instead of promises
Use async/await instead of promises because that is syntactically much easier on the eyes.
Consider the above example of a simple fetch request where you need the headers as well as the body of the request. Without async/await it might
Use async/await instead of promises because that is syntactically much easier on the eyes.
Consider the above example of a simple fetch request where you need the headers as well as the body of the request. Without async/await it might
become a mess to manage all of this in promises but with async/await you will not only have a much cleaner syntax but itβs easier to read. Try using async/await wherever you can instead of just regular promises.then() because they are usually a drop-in replacement for promises.
4. Convert callback-based APIs into promises
If you have a function that requires a callback instead of a promise being function you can easily convert it into a promise being function by creating a new function that returns a promise and resolves in the callback (callback
If you have a function that requires a callback instead of a promise being function you can easily convert it into a promise being function by creating a new function that returns a promise and resolves in the callback (callback
function is the resolver function). You can use that same functionality with async/await later once you have created a promisified version out of it.
5. Use Destructuring
Destructure code a lot. Destructuring can help you make your syntax look much more concise and clean.
Take a quick look at this example. You can either do props.a, props.b, props.c all the way down or you can extract and destruct them in a single line.
Destructure code a lot. Destructuring can help you make your syntax look much more concise and clean.
Take a quick look at this example. You can either do props.a, props.b, props.c all the way down or you can extract and destruct them in a single line.
Similarly, with arrays, you can use a similar methodology to get the individual elements out of a particular array, like an example you can see below.
So that's it from our side, which one of the tips you didn't know about?
Loading suggestions...