Skip to content

Instantly share code, notes, and snippets.

View korokd's full-sized avatar

Diogo Korok korokd

View GitHub Profile
@korokd
korokd / cloneDeep.js
Last active January 14, 2020 17:14 — forked from cassaram09/deepClone.js
JavaScript deep clone function
function cloneDeep(objSource) {
if (objSource instanceof Date) {
return new Date(objSource);
}
const plainObject = {};
if (!(objSource instanceof Object) || objSource instanceof String) {
return objSource;
}
@korokd
korokd / js_heuristics.md
Created July 19, 2019 18:40
JS heuristics (?) I discover along my way

Title

Things will be explained by examples, since this gist is primarily for myself :P

Short circuit

JavaScript short circuits Comparison Operators. Hence, you may be able to gain performance (?) by using them with this in mind.

AND

If variable a has 70% chance of being truthy and variable b has 50% chance of being truthy, you might prefer to write your if statement as if (b && a) {...} than if (a && b) {...}. Why? Because with the first, 50% of the time the engine will not even have to check for a's value