Skip to content

Instantly share code, notes, and snippets.

@dy
dy / WeakishMap.js
Created November 18, 2023 19:53
WeakishMap
// based on https://github.com/WebReflection/not-so-weak/
const refs = new WeakMap;
const set = value => {
const ref = new WeakRef(value);
refs.set(value, ref);
return ref;
};
@dy
dy / c-to-wasm.md
Last active August 18, 2023 03:05
C to WASM
@dy
dy / wasm-gotchas.md
Last active June 22, 2023 17:57
WASM gotchas

Params order

First taken from stack as block, then augmented with inline param.

(func $x (param i32 i32 i32))
...
(i32.const 0)(i32.const 1) (call $x (i32.const 3))

;; converts to
@dy
dy / web-codec-decode.js
Created January 26, 2023 14:33
Web Codec API
// web codec API (one day, hopefully)
const codecConfig = {
codec: 'mp3',
sampleRate: 48000,
numberOfChannels: 2,
description: new ArrayBuffer
}
let chunk = new EncodedAudioChunk({
type: 'key',
@dy
dy / no-typescript.md
Last active August 16, 2023 14:58
Why not typescript
  1. () => {} is not the same as () => void.
  2. {a?: string | undefined} is tautology.
  3. void | undefined | {} | never | null is tautology.
  4. Native types Record, Partial etc are a bunch of internal jargon, meaningless from JS view.
  5. Interfaces and types are same thing differently named, only creates confusion.
  6. unknown vs any: little who cares about the difference.
  7. JSX.Element, React.ReactNode, React.ReactFragment is tautology
  8. const X = {abc:123} as const
@dy
dy / no-prettier.md
Last active January 9, 2023 15:11
Why not prettier
  1. If there is //@ts-ignore on the first line, prettier may split it to multiple lines and break ts this way.
  2. Prettier expands inline constructs into ugly multiline vertical parts (myriads of examples)
  3. Prettier breaks matrices formatting
  4. It produces redundant diff logs, making it hard to analyze logic changes
@dy
dy / element-render.jsx
Last active November 16, 2022 21:38
element.render
// what if we had simple method on elements, taking in VDOM pieces?
// * it makes sure there's no internal content that messes up rendering
// * it allows simple function as input - something prohibited by preact
// * it enables element existence check
element?.render(({props}) => <div></div>);
element?.render(<div></div>);
element?.render(<Element></Element>);
function Element({props}) { ... }
@dy
dy / hyper-fragment.js
Last active September 17, 2023 20:42
Hyper/persistent document fragments
// extension of document fragment, keeps references to children
// based on https://github.com/WebReflection/document-persistent-fragment/blob/master/esm/document-persistent-fragment.js
class Bug extends DocumentFragment {}
const shenanigans = !(new Bug instanceof Bug);
export default class HyperFragment extends DocumentFragment {
#nodes = []
// DocumentFragment overrides
@dy
dy / array.wat
Last active February 7, 2022 20:15
WASM array fill performance
;; test passing array to and back
(module
(import "js" "mem" (memory 1))
(import "console" "log" (func $log (param i32 f32)))
(global $blockSize (import "js" "blockSize") (mut i32))
(global $len (mut i32) (i32.const 0))
;; len = blockSize * 4
(func (global.set $len (i32.mul (global.get $blockSize) (i32.const 4))))
(start 1)
@dy
dy / stacker.html
Created January 14, 2022 21:27
Stacker debug
<!DOCTYPE html>
<a href="https://twitter.com/jviide/status/1257755526722662405">ref</a>
<script type="module">
import t from './libs/tst.js'
// function μ(h,...d){for(var a=[],g=0,f=0,b=-1,c;c=h[g++];)c--?c--?c--?c--?c--?a[++b]=c--?c--?[d[f++],c?{}:null]:1<g||[]:d[f++]:a[b]=this(...a[b]):Object.assign(a[b][1],d[f++]):a[b-1].push(a[b--]):a[b-2][1][a[b--]]=a[b--]:a[b-1]+=""+a[b--];return a[0]}
function μ(hash, ...fields){
// [root, tag, props, ...children]
var stack=[], i=0, f=0, d=-1, c