Summary
This is a strawperson proposal to introduce direct memory allocation hints into JavaScript. Unlike the "Types as Comments" proposal, which is intentionally ignored by engines, hinted.js provides optional syntax for "blind" engine optimization.
The Problem
Writing high-performance, memory-efficient JavaScript (for AI, video processing, or games) currently requires verbose ArrayBuffer and DataView boilerplate. Engines must speculatively guess object "shapes," leading to expensive de-optimization cliffs when those shapes change.
The Solution
Provide native syntax to declare fixed-width primitives and locked object layouts. The engine is not required to "verify" these types for safety; it is simply invited to "blindly" trust them for optimized memory allocation and access.
Key Examples:
Fixed-Width Primitives:
let n: int 1 = 3; //(8-bit signed) or
let g: bit = 1; // a sigle bit
Locked Object Shapes:
type User: {
id: int 4,
flags: bit,
name: str 10
};
let player: type User = { id: 1, name: 'dragon' };
Inheritance: Functions and Arrays can extend object types to gain fixed-size .props without losing their native behavior.
Why This?
Zero-Cost Abstraction: No runtime type-checking overhead; just direct allocation.
Backward Compatible: Unsupported engines can ignore the : annotations.
Efficiency: Drastically reduces "Hidden Class" overhead and memory fragmentation.
Note on Implementation: While bit-level or sub-byte allocations might be stored as full bytes internally for performance/alignment, the engine interfaces the data as if it were the specified size. The engine is simply invited to "blindly" trust these hints to optimize the memory layout without needing to verify logic at runtime.
I am looking for feedback on the technical feasibility and for a TC39 delegate who might be interested in championing this performance-first approach.
Full Proposal & Spec: proposal/hinted.js at main · Gokul-Gireesh/proposal · GitHub