Skip to main content

Zero-Allocation String Resolution Using Binary Symbol Tables and Uint16 Interning in JavaScript

Updated 2 min readMarkdown

In high-frequency telemetry applications receiving hundreds of market updates per second, repeatedly decoding UTF-8 strings via TextDecoder for recurring league names, status labels, or market codes creates unnecessary garbage collection pressure on low-end mobile devices.

To eliminate hot-path string allocations completely, synchronize a compact symbol dictionary on connection startup and transmit 16-bit integer symbol IDs in live binary frames:

// protocol/binarySymbolTable.js
export class BinarySymbolDictionary {
  constructor(maxSymbols = 4096) {
    // Flat array indexed directly by 16-bit integer ID for O(1) zero-alloc lookup
    this.symbols = new Array(maxSymbols).fill('');
    this.decoder = new TextDecoder('utf-8');
  }

  /**
   * Ingests a compact dictionary packet received once during handshake
   * Wire format per entry: [Uint16 id][Uint8 byteLen][UTF-8 bytes...]
   */
  loadDictionaryBuffer(arrayBuffer) {
    const view = new DataView(arrayBuffer);
    const bytes = new Uint8Array(arrayBuffer);
    let offset = 0;

    while (offset < bytes.byteLength) {
      const symbolId = view.getUint16(offset, true);
      const strLen = view.getUint8(offset + 2);
      offset += 3;

      const strSlice = bytes.subarray(offset, offset + strLen);
      this.symbols[symbolId] = this.decoder.decode(strSlice);
      offset += strLen;
    }
  }

  resolve(symbolId) {
    return this.symbols[symbolId] || 'UNKNOWN';
  }
}

// Example usage: Resolving live market updates with zero string allocations
export const symbolDict = new BinarySymbolDictionary();

export function processTelemetryFrame(frameBuffer, targetElement) {
  const view = new DataView(frameBuffer);
  // Frame layout: [Uint16 leagueSymbolId][Uint16 marketSymbolId][Float32 odds]
  const leagueId = view.getUint16(0, true);
  const marketId = view.getUint16(2, true);
  const liveOdds = view.getFloat32(4, true);

  // O(1) array lookup — zero TextDecoder invocations, zero heap allocations
  const leagueName = symbolDict.resolve(leagueId);
  const marketLabel = symbolDict.resolve(marketId);

  if (targetElement) {
    targetElement.textContent = `${leagueName} | ${marketLabel}: ${liveOdds.toFixed(2)}`;
  }
}

Key Engineering Advantages:

  • Zero Hot-Path String Allocations: Moves UTF-8 decoding entirely to the initial handshake phase, reducing per-frame memory allocations to zero during live telemetry bursts.
  • Compact Wire Payloads: Replaces repetitive multi-byte string keys with 2-byte Uint16 identifiers, conserving cellular bandwidth on metered mobile connections.

For an operational reference of decoupled, low-footprint web viewports running in production, inspect this reference node.

No comments yet. Be the first to comment.