DEV Community

Tether
Tether

Posted on • Updated on

Tech Hacks discovered dailly

List of all Tech hacks I find dailly while reading a blog or while reviewing code of peers.

Mostly will be related JavaScript, React JS, Node JS, Angular, TypeScript, GIT , HTML and CSS

Just making a list:

1) axios setting deafault header for all request
axios.defaults.headers.common["authorization"] = encodedToken;

2) Function Declarations get Hoisted to Top of the context, where as classes declarations and function Expressions are not Hoisted.

3) Way to round a number

x = -3.45
console.log(~~x) // -3
console.log(x | 0) // -3
console.log(x << 0) // -3

4)How JS can find an element in a Map object in one step?

Map uses a hashtable (or a similar mechanism) as specified in the spec. Therefore, when an object is requested a hash is calculated. Then the specific location in an internal table is located. This is the basic theory about hash tables and why they are usualy O(1). Do note, that if there are many collisions performance can move towards O(N).

5)
The CSS @import directive has a major drawback due to performance issues; it creates an extra HTTP request each time you call it. However, the Sass @import directive includes the file in the CSS; so no extra HTTP call is required at runtime!

6) which is faster ifelse or switch in js
Compiler is better in optimizing switch-statement then if-else because of the order of evaluation in if is not stacked based on importance but in switch it knows the clause to be evaluated together and with more efficient order.
-> Stats http://www.blackwasp.co.uk/speedtestifelseswitch.aspx
Image description

7) Object.is() vs ===
Object.is it for comparing 2 values and default is true
null and null
undefined and undefined
are equal
but in non primitive data type if not referencing to same instance in memory then not not equal like
Object.is({},{}) or Object.is([],[])

but then what is the difference between === and Object.is()
the difference is signed 0 and Nan
so
a) Object.is(NaN,Nan) => true
NaN === NaN => false
b) Object.is(NaN,0/0) => true
NaN === 0/0 => false
c) Object.is(NaN,Number.NaN) => true
NaN === Number.Nan => false

Top comments (1)

Collapse
 
liliang8858 profile image
edwin_ew

Nice