absorb.md

Amjad Masad

Chronological feed of everything captured from Amjad Masad.

RequireBin Enables Browserify-style NPM Module Bundling for Instant Buffer API Demos

RequireBin provides a live coding environment where developers can require NPM modules like 'buffer' directly in the browser, automatically bundling them via browserify-cdn. The demo creates a 100-byte Buffer instance and verifies that subarray(50,100) returns a 50-byte view, demonstrating efficient array slicing without data copying. This showcases polyfill Buffer implementation with full Node.js API compatibility including read/write methods, encoding support, and TypedArray augmentation when available.

Amjad Masad's Minimalist Gist Encapsulates "Clowntown" as Sole Content

Amjad Masad published a GitHub Gist titled /clowtown.js containing HTML/JavaScript that embeds another Gist. The embedded Gist displays a single line of text: "clowntown". This structure serves as a self-referential or humorous placeholder with no functional code beyond rendering the term.

Node.js Fails to Reclaim Unused Heap Memory After Explicit GC

In Node.js v0.12 with --expose-gc, loading and transforming a large Babel browser bundle consumes 430 MB heap, which drops to 19.8 MB immediately after gc(). However, heapTotal remains fixed at 240 MB while heapUsed slowly increases over time without further allocations. This demonstrates V8's heap allocator does not shrink committed memory post-GC, leading to persistent high RSS usage.

Node.js Manual GC Effectively Reclaims Massive Heap Allocations of Object Arrays

Node.js script allocates 20 million empty objects in a loop, spiking heapUsed from ~3.5 MB to ~782 MB. Explicit gc() call then reclaims nearly all allocated memory, dropping heapUsed back to ~3.4 MB consistently across repeated cycles. Demonstrates V8's garbage collector handles large, short-lived object arrays with high efficiency when manually triggered.

CSP Channels Replace State Machines for Sequential RPC Client Buffering

js-csp channels enable a concise, generator-based implementation of a client that buffers requests to a single-threaded RPC math worker process, avoiding explicit state variables and queues. The core loop awaits ready signals, serializes requests via channels, and dispatches responses without manual state transitions. This contrasts with traditional state machine approaches using enums and queues, offering better locality for multi-turn interactions like iterative number guessing.

Flow Type Checker Fails Null Check on Class Properties Despite Working on Parameters

In Flow, checking a nullable parameter with `if (n == null)` correctly narrows it to non-null, enabling safe use like `n > 10`. The same pattern fails for class properties, where `if (this.n == null)` does not narrow `this.n : ?number`, causing type errors on `this.n > 10`. This reveals an inconsistency in Flow's type narrowing for `this` properties versus function parameters.

Temporal Dead Zone Edge Cases in JavaScript Default Parameter Evaluation Order

Amjad Masad explores TDZ violations in JavaScript default parameters when closures reference later parameters. He questions if early references (e.g., bar referencing x) are allowed due to hoisting equivalence, but suspects mutual dependencies (e.g., bar referencing x=bar()) should throw. Nested TDZ in closure parameters (e.g., function(y=x)) likely triggers errors, necessitating static TDZ checks for transpilation.

Racket Implementation of Conway's Game of Life with 2htdp Universe Animation

This Racket code implements Conway's Game of Life using 2htdp libraries for a grid-based cellular automaton. It defines cell and board structures, neighbor counting via bounded iteration, and applies standard survival/birth rules: live cells survive with 2-3 neighbors, die otherwise; dead cells birth with exactly 3 live neighbors. The board animates via big-bang with on-tick updates at 0.25s intervals, rendering scaled black squares on white background for live cells, including a cross pattern initializer.

JavaScript Snippet Forces GIF Playback on Facebook by Replacing Thumbnail Images

This self-executing JavaScript function scans Facebook links ending in .gif, replacing their img src attributes with the direct GIF URL to trigger native animation. It uses a debounced scroll event handler (300ms delay) to efficiently reprocess links as the user scrolls, ensuring animated thumbnails load dynamically. Initial execution runs on load, targeting only links with matching href patterns and excluding those with onmouseover handlers containing 'gif' by dispatching the event instead.

Implementing Lisp cons, car, cdr in JavaScript with Pure Functions

Cons constructs a pair by returning a selector function that applies its argument to the pair's components. Car and cdr extract the first and second elements by passing selector functions that return a or b respectively. The implementation supports nested pairs, demonstrated by cons(1, cons(2, cons(3, null))) yielding expected car/cdr results.

Proxy-Based Object Simulates Adam with getName Returning "Adam of Eden"

This GitHub Gist demonstrates ES6 Proxy usage to intercept property access on an object named 'adam'. The Proxy's get trap handles the 'getName' message by returning "Adam of Eden", throwing an error for unknown messages. It showcases basic Proxy interception without a target object via Proxy.create (pre-Proposal syntax).

Lightweight JavaScript Router with Nested Scoping and Regex Params

This JS router parses window.location.pathname into segments and matches against route patterns using a stack-based arg collector for params and nested scopes. It supports optional path segments via parentheses, parameterized routes with :names, and regex constraints via regHash objects. Core APIs—scope (partial match with path advancement), match (full path consumption), part (partial match without advancement)—enable hierarchical routing as shown in the '/en/sections/123abc/1' example.