Skip to content

Instantly share code, notes, and snippets.

View johnborges's full-sized avatar
Focusing

John Borges johnborges

Focusing
View GitHub Profile
rm -rf ~/Library/Caches/CocoaPods
rm -rf Pods
rm -rf ~/Library/Developer/Xcode/DerivedData
pod deintegrate
pod setup
pod install
@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 },
};
@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 / 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.

String to Number

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

my_string = "amazing";
console.log(+my_string);
// NaN
@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();
@johnborges
johnborges / symbols.js
Created January 7, 2020 21:31
JavaScript Symbols
coming soon
@johnborges
johnborges / js-parsing.md
Created December 2, 2019 19:54
Faster JavaScript Apps with JSON.parse()

Because the JSON grammar is much simpler than JavaScript’s grammar, JSON can be parsed more efficiently than JavaScript. This knowledge can be applied to improve start-up performance for web apps that ship large JSON-like configuration object literals (such as inline Redux stores).

const data = { foo: 42, bar: 1337 }; // 🐌
const data = JSON.parse('{"foo":42,"bar":1337}'); // 🚀
@johnborges
johnborges / js-interview.md
Last active December 11, 2020 20:40
7 Tricky JavaScript Interview Questions

7 Tricky JavaScript Interview Questions

  1. Accidental Global
function foo() {
  let a;
  window.b = 0;
  a = window.b;
  a++;
  return a;
@johnborges
johnborges / vanilla-redux.js
Last active November 9, 2019 02:27
Simple Redux Implementation without any library.
// simplified createStore function
const createStore = (reducer) => {
let listeners = [];
let currentState = reducer(undefined, {});
return {
getState: () => currentState,
dispatch: (action) => {
currentState = reducer(currentState, action);
listeners.forEach((listener) => {
listener();