Skip to content

Instantly share code, notes, and snippets.

View rpivo's full-sized avatar

Ryan Pivovar rpivo

View GitHub Profile
@rpivo
rpivo / index.md
Last active December 6, 2020 06:23
process.memoryUsage() in Node
@rpivo
rpivo / index.md
Last active December 6, 2020 17:26
Your Code (Including Comments) Takes up Runtime Memory

Your Node.js Code (Including Comments) Takes up Runtime Memory

In the Node example below, we are logging the heapUsed property that's returned from process.memoryUsage(), and we divide this value by 1000, giving us the amount of allotted memory heap in kilobytes that we used to run this process.

console.log(`${process.memoryUsage().heapUsed / 1000}kb`)

// output: 2994.304kb
@rpivo
rpivo / index.md
Last active December 6, 2020 18:26
Variable Declarations & Hoisting

Variable Declarations & Hoisting

Because all our global declarations are already registered in memory during compilation and prior to runtime, we can access these declarations anywhere in our code, even before they formally have been declared.

//we can use these variables even though they haven't formally been declared yet in this scope
console.log(a === b) // true
console.log(b === c) // true
console.log(c === d) // true
@rpivo
rpivo / index.md
Last active December 7, 2020 19:25
Comparing Exact Object Types in Flow & TypeScript

Comparing Exact Object Types in Flow & TypeScript

Flow has something called exact object types. Without using it, you can specify properties that an object should have, but there's nothing stopping the object from having other properties besides the ones specified.

/* @flow */

const obj: {
  foo: string,
  bar: string,
@rpivo
rpivo / index.md
Last active December 8, 2020 02:59
Comparing Generics in Flow & TypeScript

Comparing Generics in Flow & TypeScript

The use of generics in Flow and TypeScript is pretty similar, except for a few things that are mentioned at the end. The following examples focus on TypeScript.

This simple example uses generics to specify a function that takes in three arguments wherein the last two must be of the same type.

function createTuple<A, B>(a: A, b: B, c: B): [A, B, B] {
  return [a, b, c]
}
@rpivo
rpivo / index.md
Last active December 9, 2020 00:39
Comparing Never & Unknown Types in Flow & TypeScript

Comparing Never & Unknown Types in Flow & TypeScript

Although not fully documented, Flow has a type called empty that is somewhat similar to TypeScript's never type.

In TypeScript, we can use never to indicate a return type that will never happen, or to indicate an otherwise impossible branch of flow.

function neverReturns(): never {
  while (true) {}
}
@rpivo
rpivo / index.md
Last active December 9, 2020 01:38
Comparing Read-Only & Write-Only Types in Flow & TypeScript

Comparing Read-Only & Write-Only Types in Flow & TypeScript

Flow and TypeScript have very similar means for declaring read-only types.

In TypeScript, if we want to create an object with immutable properties, we can do this:

const obj: Readonly<{
  foo: string,
  bar: string,
@rpivo
rpivo / index.md
Last active December 10, 2020 05:46
Sync Methods in Node

Sync Methods in Node

It's very easy to get into callback hell in Node if the programmer needs to block the process and chooses not to use Node's sync methods (or its promise methods).

Since Node is made to deal with lots of streamed data, and since these streams have an unknown size and can take an unknown amount of time to complete, much of Node's functionality is built with callbacks to block parts of the process.

Even just simulating an arbitrary and minimal example of this, it's clear how obnoxiously complicated this can get.

function callFirst(cb) {
@rpivo
rpivo / index.md
Last active December 10, 2020 15:38
Public & Private Access Modifiers in C++

Inside C++ classes, access modifiers like public and private are written similarly to Python branches like if, else, and for statements.

Public & Private Access Modifiers in C++

class Thing {
  public:
    int x;
  private:
 int y;
@rpivo
rpivo / index.md
Last active December 11, 2020 14:24
const in C++

const in C++

In C++, we can declare a value as immutable by using the const keyword, which is similar to const in JavaScript.

bool isVowel(const char p_char) { ... }

Unlike JavaScript, we can label incoming arguments with const, indicating that they won't undergo any mutation throughout the function.