Skip to main content

Try it Live

Run Denomination examples in the interactive playground

WASM Implementation

WebAssembly-accelerated implementations of denomination conversions, compiled from Zig using ReleaseSmall mode.

Overview

WASM implementations provide performance-critical denomination operations using compiled Zig code targeting WebAssembly. These are purely opt-in replacements for the JavaScript implementations with identical APIs. All WASM methods are compiled from denomination.zig with ReleaseSmall optimization targeting minimal bundle size.

Quick Start

Performance

WASM implementations provide significant speedup for:
  • U256 arithmetic - Compiled Zig multiplication and division
  • Bulk conversions - Processing many values in tight loops
  • Constant-time operations - No JavaScript overhead
Use WASM when processing many denomination conversions (gas calculations, bulk balance conversions). For single operations, JS overhead may outweigh WASM benefits.

API Reference

WASM implementations match the standard Denomination API. See main documentation:
  • Wei - from, fromGwei, fromEther, toGwei, toEther, toU256
  • Gwei - from, fromWei, fromEther, toWei, toEther, toU256
  • Ether - from, fromWei, fromGwei, toWei, toGwei, toU256

WASM-Accelerated Methods

All denomination conversions implemented in Zig:

Wei

Gwei

Ether

Usage Examples

Bulk Gas Calculations

Balance Conversions

Real-Time Gas Price Tracking

Performance Characteristics

Multiplication (Upscaling)

Conversions that multiply are memory-bound:
  • Gwei.toWei - multiply by 10^9
  • Ether.toWei - multiply by 10^18
  • Ether.toGwei - multiply by 10^9
WASM benefit: Moderate (10-30% faster) - limited by memory access.

Division (Downscaling)

Conversions that divide are compute-bound:
  • Wei.toGwei - divide by 10^9
  • Wei.toEther - divide by 10^18
  • Gwei.toEther - divide by 10^9
WASM benefit: Significant (2-5x faster) - Zig division highly optimized.

Files

  • denomination.zig - Zig source implementation
  • Wei.wasm.ts - TypeScript WASM bindings for Wei
  • Gwei.wasm.ts - TypeScript WASM bindings for Gwei
  • Ether.wasm.ts - TypeScript WASM bindings for Ether

Zig Tests

The Zig implementation includes comprehensive tests:
Run tests:

Benchmarking

Compare JS vs WASM performance:

Limitations

WASM implementations have same behavior as JS:
  • Integer division - Fractional results truncate
  • No overflow checks - U256 operations wrap
  • Same API - Drop-in replacement
  • Wei - Wei API reference
  • Gwei - Gwei API reference
  • Ether - Ether API reference
  • Conversions - Conversion details
  • Uint WASM - Underlying U256 WASM implementation