Tips from JS Tips (WIP)

Just a reminder of some useful tips from JS Tips
http://www.jstips.co/en/insert-item-inside-an-array/


Array Performance

arr.push(ele); // slower
arr[arr.length] = ele; // faster
arr.unshift(ele); // slower
[ele].concat(arr); // faster

React Key Prop

//bad
...
render() {
<div key=></div>
}
...

//good
<MyComponent key=/>

Using array index as key is an anti-pattern, using unique id is better.

Nested Conditional

if — else:

if (condition) {
if (condition === '1') {
do1();
} else if (condition === '2') {
do2();
} else if (condition === '3') {
do3();
} else {
doDefault();
}
}

switch:

switch (condition) {
case '1':
do1();
break;
case '2':
do2();
break;
case '3':
do3();
break;
default:
doDefault();
break;
}

object literal:

var doThings = {
'1': do1,
'2': do2,
'3': do3,
'default': deDefault
};
doThings[condition || 'default']();

undefined and null

typeof undefined === 'undefined' // true
typeof null === 'object' // true
null == undefined // true
null === undefined // false