Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
gordonbrander / derp-modules.js
Created October 9, 2012 22:06
Simple, tiny, dumb JavaScript Modules
// Simple, tiny, dumb module definitions for Browser JavaScript.
//
// What it does:
//
// * Tiny enough to include anywhere. Intended as a shim for delivering
// browser builds of your library to folks who don't want to use script loaders.
// * Exports modules to `__modules__`, a namespace on the global object.
// This is an improvement over typical browser code, which pollutes the
// global object.
// * Prettier and more robust than the
@gordonbrander
gordonbrander / n-vector.js
Last active October 29, 2023 13:00
n-vector: generic, efficient JavaScript vector math using ordinary arrays or indexed objects
/*
n-vector.js
Copyright (c) 2014 Gordon Brander
Released under the MIT license
http://opensource.org/licenses/MIT
Generic, efficient JavaScript vector math using ordinary arrays
or indexed objects.
@gordonbrander
gordonbrander / singledispatch.js
Last active October 19, 2023 13:57
singledispatch.js - single dispatch generic function like Python's singledispatch
// Create a Python singledispatch / Clojure multimethod style function that
// dispatches on the first argument.
//
// Returns a wrapped function that calls appropriate underlying function
// based on prototype of first argument.
//
// Custom implementations for specific types can be registered through calling
// `.define(constructor, fun)` on the wrapped function.
export const singledispatch = fallback => {
let _key = Symbol('singledispatch method')
@gordonbrander
gordonbrander / h.js
Last active October 5, 2023 23:04
h.js - hyperscript micro implementation
// Set key on object, but only if value has changed.
// This is useful when setting keys on DOM elements, where setting the same
// value may trigger a style recalc.
//
// Note that the typical layout-triggering DOM properties are read-only,
// so this is safe to use to write to DOM element properties.
// See https://gist.github.com/paulirish/5d52fb081b3570c81e3a.
export const prop = (object, key, value) => {
if (object[key] !== value) {
object[key] = value
@gordonbrander
gordonbrander / prop.js
Created September 28, 2023 02:15
prop.js - make DOM fast with a write-through cache
// Set property of element.
// Uses a write-through cache to only set
// when value actually changes.
export const prop = (el, key, value) => {
let cacheKey = Symbol.from(`prop.${key}`)
if (el[cacheKey] != value) {
el[cacheKey] = value
el[key] = value
}
}
@gordonbrander
gordonbrander / multimethods.lua
Last active August 29, 2023 14:34
Lua multimethods
--[[
Lua multimethods. Inspired by Clojure's multimethods.
How to use it:
local Multi = require('multimethods')
-- Create a new multimethod and use isa as dispatch function.
-- This will dispatch on first argument by type.
local foo = Multi.create(isa)
@gordonbrander
gordonbrander / async-mailbox.js
Created May 14, 2023 00:46
async-mailbox.js - async generator publisher
export const sleep = ms => new Promise(resolve => {
setTimeout(resolve, ms)
})
export const hz = x => (1/x * 1000)
const complete = Symbol('complete')
export class Mailbox {
#buffer = []
@gordonbrander
gordonbrander / kv.js
Last active May 4, 2023 22:31
KV: key-value iteration functions for JavaScript
// KV.js
// Hash iteration functions
// Set a value on an object at field, returning object.
function set(object, key, value) {
// Set value on object at key.
object[key] = value;
// Return object.
return object;
}
@gordonbrander
gordonbrander / README.md
Last active April 25, 2023 20:38
microview - data-driven views that render once per frame, at most

Microview

What if... Virtual DOM... but by hand?
aha ha, just kidding...
unless.. ?

Microview is a tiny library for writing efficient data-driven DOM rendering logic by hand. DOM writes are driven by a pure data model. Using Microview, you can freely "bash the dom". Writes will be batched — they'll only happen once per animationframe, and only if the data model changes.

@gordonbrander
gordonbrander / cdom.js
Created March 26, 2023 01:41
cdom.js - cached dom writers that only write if property value changed
// CDOM - Cached DOM
// CDOM minimizes DOM writes by caching last written value and only touching the
// DOM when a new value does not equal the cached value.
// The goal of CDOM is to make it safe to "bash the DOM" as often as you like
// without affecting performance, even bashing it every frame.
// Create a cached setter
export const CachedSetter = (ns, set) => (el, value) => {
let cacheKey = Symbol.for(`CachedSetter::${ns}`)
if (el[cacheKey] !== value) {