Skip to content

Instantly share code, notes, and snippets.

View johnborges's full-sized avatar
Focusing

John Borges johnborges

Focusing
View GitHub Profile
@johnborges
johnborges / js-patterns.md
Created May 6, 2020 16:27
7 Common JS Design Patterns
  1. Constructor
// This creates a new empty Object
var newObject = {};

// This creates a new empty Object
var newObject = Object.create(Object.prototype);

var newObject = new Object();

String to Number

my_string = "123";
console.log(+my_string);
// 123

my_string = "amazing";
console.log(+my_string);
// NaN
@johnborges
johnborges / js-mem-leaks.md
Last active January 15, 2021 16:43
Common causes for memory leaks in Web Apps.

JS Memory Leaks - Common Sources

  1. addEventListener. This is the most common one. Call removeEventListener to clean it up.

  2. setTimeout / setInterval. If you create a recurring timer (e.g. to run every 30 seconds), then you need to clean it up with clearTimeout or clearInterval. (setTimeout can leak if it’s used like setInterval – i.e., scheduling a new setTimeout inside of the setTimeout callback.)

  3. IntersectionObserver, ResizeObserver, MutationObserver, etc. These new-ish APIs are very convenient, but they are also likely to leak. If you create one inside of a component, and it’s attached to a globally-available element, then you need to call disconnect() to clean them up. (Note that DOM nodes which are garbage-collected will have their listeners and observers garbage-collected as well.

@johnborges
johnborges / fetch-cancel.js
Last active January 26, 2021 19:10
Cancel a Fetch Request
const controller = new AbortController();
const { signal } = controller;
fetch("http://localhost:8000", { signal }).then(response => {
console.log(`Request 1 is complete!`);
}).catch(e => {
console.warn(`Fetch 1 error: ${e.message}`);
});
fetch("http://localhost:8000", { signal }).then(response => {
@johnborges
johnborges / conditional-obj.js
Created June 5, 2021 12:42
The shortest way to conditionally insert properties into an object literal.
const obj = {
...condition && { prop: value },
};
rm -rf ~/Library/Caches/CocoaPods
rm -rf Pods
rm -rf ~/Library/Developer/Xcode/DerivedData
pod deintegrate
pod setup
pod install