Skip to content

Instantly share code, notes, and snippets.

View bellentuck's full-sized avatar

Ben Ellentuck bellentuck

  • New York, NY
View GitHub Profile
@bellentuck
bellentuck / pep-revised-draft-5-6-18.md
Last active May 7, 2018 00:25
React-Redux data management: serialization and memoized caching of derived data

React-Redux data management: serialization and memoized caching of derived data

Minimizing lookup times is a central problem in data management, particularly for large applications. Linear lookup time is bad. Sublinear lookup time is better. Constant lookup is ideal.

Client-side, in the context of a Redux-managed application, the problem becomes a matter of defining the shape of app state.

This article explores two avenues for better organizing such front-end state: (1) serializing data from the back end, reducing to fully-indexed state shape via utility reducers, and (2) caching data on the front end, in particular by utilizing the Reselect library in React-Redux mapStateToProps functions to create or look up "memos" of chunks of derivable data.

//run this in your console
for (var i = 0; i <= 3; i++) {
  setTimeout(function(){
    console.log(i); 
  }, 0); 
} 
@bellentuck
bellentuck / call_all.js
Created February 5, 2018 22:03
calling all args! xoxo, generator
function* cycleThruFnArgs(obj, fnArr) {
while (fnArr.length > 0) {
yield fnArr.shift().call(obj);
}
}
function callAll(obj, args) {
if (arguments.length < 2) throw 'Supply obj and args array';
var gen = cycleThruFnArgs(obj, args);
@bellentuck
bellentuck / isFalsy.js
Created January 26, 2018 17:07 — forked from skoshy/isFalsy.js
Determines if any value is "falsy" in JavaScript
// this function takes in any parameter and can tell if it's "falsy" or not.
// falsy means it's either false, 0, null, undefined, NaN, or an empty string/array/object
// see the test cases at the bottom for a clearer picture
function isFalsy(item) {
try {
if (
!item // handles most, like false, 0, null, etc
|| (
typeof item == "object" && (
@bellentuck
bellentuck / types.js
Created January 24, 2018 19:14 — forked from pbakondy/play.js
Play with Object.prototype.toString.call()
// under Google Chrome 36
Object.prototype.toString.call([])
// "[object Array]"
Object.prototype.toString.call(function(){})
// "[object Function]"
Object.prototype.toString.call({})
// "[object Object]"
Object.prototype.toString.call(null)
// "[object Null]"
@bellentuck
bellentuck / vector.js
Created January 23, 2018 03:30 — forked from kj786/vector.js
Implement getters and setters to ease access to private members of Vector.
/*global console: true */
"use strict";
/*
A vector type
Write a constructor Vector that represents a vector in two-dimensional
space. It takes x and y parameters (numbers), which it should save to
properties of the same name.
Give the Vector prototype two methods, plus and minus, that take another vector as a parameter and return a new vector that has the sum
or difference of the two vectors’ (the one in this and the parameter) x
and y values.
@bellentuck
bellentuck / PredicateCombinators.js
Created December 14, 2017 16:46
JS predicate combinator boilerplate. Riffing off Jack Firth's "Predicates in Javascript"
// Firth: https://codepen.io/Universalist/post/predicates-in-javascript
(function(predicateCombinators) {
// CPL predication
function and(p1, p2) {
return function(x) {
return p1(x) && p2(x);
}
copyright: mit
copyright: mit
copyright: mit