Javascript
Parallelize Work with Workers in Javascript
· ☕ 2 min read
Use workers when doing lot of work that can be spawned off in another “thread”. How do you open parallel work threads today? Possibly using async functions? Async functions do not block the main thread but allow processing to take place in parallel. Well, almost in parallel - see how event loops work.

Cloning objects in Javascript
· ☕ 3 min read
Cloning objects is easier than ever before, but you have to remember a few nuances. We will discuss about two ways to clone/copy an object - Deep copy: copy object so that any subsequent changes to new object does not reflect in older object Shallow copy: copy props and child objects of an object, but the new object keeps pointing to the original Both have their uses.

Function returns undefined unless specified otherwise
· ☕ 1 min read
Why is my function returning undefined when I have not mentioned that anywhere. The crux of the issue is in the topic header. All functions have to return something. If nothing is specified, they return undefined. 1 2 3 4 5 6 7 function doSomething() { // do something } const something = doSomething(); console.

Why use JSON.stringify() in debugging statements?
· ☕ 2 min read
Does JSON.stringify() have anything to do in debugging statements today? There were many Javascript-like systems that did not have the same tooling or debugging methods. I do not quite remember having browsers or ‘node’ print out the entire object back in the day - I may be wrong. So it was that I had to use console quite a bit, and what better way to print objects other than converting them to strings?

Use Strict in Inherited Javascript Code
· ☕ 2 min read
You should use strict mode everywhere, but what about inherited code? Typically, you designate strict mode at the beginning of the module - 1 "use strict"; But the module and its friends may have millions of lines of code that you did not write and do not have time to fix.

Assign values vs. push in Javascript arrays
· ☕ 1 min read
There is this myth that assigning array elements is better performant than push. I believe this originated from Javascript of yore, but is not relevant anymore. In fact, today push may be almost equal to or edge out assignment of array elements using keys (depending on runtime engine that is).

Nullify prop value vs deleting key in an object
· ☕ 3 min read
It is common advise to nullify a prop value within an object rather than deleting the prop. Should you prefer one over the other? Especially, in performance intensive applications. Delete object prop What do you do when you do not need a key-value pair in an object? Why, we simply delete it, of course.

Use homogenous arrays to maintain sanity
· ☕ 1 min read
Use values with same types in any array. Changing types (even for scenarios that you think are totally valid for the day) can rain down hell. Homogeneity of an array refers to using the same types of variables within an array. The following arrays are homogenous - 1 2 3 4 const fruits = ['apple', 'orange]; const nums = [0,1,2] const answers= [true, false]; The following arrays are not -

Is it really useful to cache array length in a for loop?
· ☕ 2 min read
It is a commonly recommended practice to cache array lengths while doing for loops and get better performance. Seriously, is that a thing though? Typical for loop In a typical for, you calculate length, iterate and you are done. 1 2 3 4 5 6 7 8 9 10 const arr = new Array(1e7).

Shortcuts to variable assignments in Javascript
· ☕ 2 min read
You don’t have to see assigning values to variables as a chore. There are exciting ways to do those assignments - here are some shorthand ways. Initialize to same value You can initialize variables like so - 1 2 3 let i = 0; let j = 0; let k = 0; Or, you can instead do -

Using strict mode in Javascript
· ☕ 2 min read
‘use strict’ is a rather useful statement to have at the beginning of all Javascript modules or functions. It ensures that all code in the module is executed in strict mode. The usage is simple enough. 1 2 3 4 "use strict"; // lot of code You can also enable strict mode to specific functions.

Avoid Scope Pollution in Javascript
· ☕ 1 min read
It is quite easy to pollute global scope by defining variables or functions that impact core functions. 1 2 3 4 const parseInt = val => console.log("int parsed: ", val); parseInt(1); // int parsed: 1 parseInt has transformed into a printer, thanks to the above code. The practice of “overriding” global scope is not useful and should be avoided like the plague (If you are born in the wrong century - “plague is to be avoided at all costs”).

Use Right Syntax for a Map
· ☕ 2 min read
Map on an array is simple enough - 1 2 3 const ran = ["1", "3", "5"].map(val => Number(val)); console.log("ran: ", ran); // ran: [ '1', '3', '5' ] map will specify one element at a time, which is passed to the callback function as val. Let’s say, we ignore the val.