Bloxwapdocs
Developers

Low-latency recipes

Optimize signing performance with WASM secp256k1 and fast local wallets.

Reduce order latency for high-frequency trading agents with these optimization techniques.

WASM secp256k1 signing

The @bloxwap/hyperliquid SDK automatically uses WASM-accelerated crypto when available. The SDK includes optional dependencies for:

  • tiny-secp256k1 — WASM secp256k1 signing (3-5x faster than pure JavaScript)
  • hash-wasm — WASM hashing for signature operations

These are installed automatically with the SDK. No additional configuration needed.

Installation

npm install @bloxwap/hyperliquid

The SDK will automatically use WASM implementations when available, falling back to JavaScript if WASM is unavailable.

Verify WASM acceleration

Check if WASM modules loaded successfully:

import { HttpTransport, ExchangeClient } from '@bloxwap/hyperliquid';
import { createFastLocalWallet } from '@bloxwap/hyperliquid/signing';

// Create a fast local wallet (automatically uses WASM when available)
const wallet = await createFastLocalWallet(privateKey);

// Benchmark signing performance
const start = performance.now();
const client = new ExchangeClient({
  transport: new HttpTransport(),
  wallet: wallet,
});

// Place a test order (testnet recommended)
await client.limitOrder({
  coin: 'BTC',
  isBuy: true,
  sz: 0.001,
  limitPx: 50000,
  orderType: { limit: { tif: 'Gtc' } },
});

const elapsed = performance.now() - start;
console.log(`Order latency: ${elapsed.toFixed(2)}ms`);

Expected latency (local signing only, excludes network):

  • With WASM: 5-15ms for signature generation
  • Without WASM: 20-50ms for signature generation

Fast local wallets

Use createFastLocalWallet instead of viem's privateKeyToAccount for optimal performance.

import { createFastLocalWallet } from '@bloxwap/hyperliquid/signing';

// ✅ Fast: Direct secp256k1 signing
const fastWallet = await createFastLocalWallet(privateKey);

// ❌ Slower: JSON-RPC overhead
import { privateKeyToAccount } from 'viem/accounts';
const slowWallet = privateKeyToAccount(privateKey);

createFastLocalWallet signs directly with secp256k1, bypassing viem's JSON-RPC layer. This reduces signature latency by 50-70%.

Signature warm-up

Pre-warm the signing path to eliminate first-signature overhead.

import { warmupSigning } from '@bloxwap/hyperliquid/signing';

const wallet = await createFastLocalWallet(privateKey);

// Warm up once at startup
await warmupSigning(wallet);

// First signature is now fast

warmupSigning pre-initializes:

  • WASM modules (if available)
  • Signature buffers
  • Crypto contexts

This eliminates 50-200ms of cold-start overhead on the first order.

Connection pooling

Reuse HTTP connections for multiple orders.

import { HttpTransport, ExchangeClient } from '@bloxwap/hyperliquid';

// Create one transport for the lifetime of your agent
const transport = new HttpTransport({
  isTestnet: false,
  // Optional: custom fetch with connection pooling
  fetch: customFetch,
});

// Reuse transport across orders
const client = new ExchangeClient({
  transport: transport,
  wallet: wallet,
});

// Place multiple orders without reconnecting
for (const order of orders) {
  await client.limitOrder(order);
}

Batch order placement

Place multiple orders in one request when possible.

// ❌ Slower: Multiple HTTP requests
for (const order of orders) {
  await client.limitOrder(order);
}

// ✅ Faster: Batch request (if SDK supports)
// Check SDK documentation for batch order methods

Network optimization

Use low-latency hosting

  • Co-location: Host agents in the same AWS region as Hyperliquid's API
  • Geographic proximity: Choose the region closest to Hyperliquid's servers
  • Dedicated servers: Avoid shared hosting for consistent network performance

DNS caching

Pre-resolve DNS to eliminate lookup latency:

import dns from 'node:dns';

// Pre-resolve Hyperliquid API domains at startup
dns.resolve('api.hyperliquid.xyz', (err, addresses) => {
  console.log('Hyperliquid API IPs:', addresses);
});

HTTP/2 and keep-alive

Modern Node.js (20+) enables HTTP/2 by default. Ensure your fetch implementation supports it:

import { fetch } from 'undici';

const transport = new HttpTransport({
  fetch: fetch, // undici supports HTTP/2 and connection pooling
});

Order type optimization

Choose order types that minimize latency:

Limit orders with IOC

Immediate-or-cancel orders execute or cancel immediately, reducing state management:

await client.limitOrder({
  coin: 'BTC',
  isBuy: true,
  sz: 0.1,
  limitPx: 50000,
  orderType: { limit: { tif: 'Ioc' } }, // Immediate-or-cancel
});

Market orders for speed

Market orders execute immediately at the best available price:

await client.marketOrder({
  coin: 'BTC',
  isBuy: true,
  sz: 0.1,
  slippage: 0.01, // 1% max slippage
});

Benchmarking

Measure and log latency for each component:

async function placeOrderWithBenchmark() {
  const t0 = performance.now();

  // Signing latency
  const signStart = performance.now();
  // ... sign order ...
  const signEnd = performance.now();
  console.log(`Sign: ${(signEnd - signStart).toFixed(2)}ms`);

  // Network latency
  const netStart = performance.now();
  const response = await client.limitOrder(/* ... */);
  const netEnd = performance.now();
  console.log(`Network: ${(netEnd - netStart).toFixed(2)}ms`);

  const total = performance.now() - t0;
  console.log(`Total: ${total.toFixed(2)}ms`);

  return response;
}

Track these metrics over time to identify regressions or optimization opportunities.

Monitoring and alerts

Set up alerts for latency degradation:

const LATENCY_THRESHOLD_MS = 100;

if (latency > LATENCY_THRESHOLD_MS) {
  // Alert: Latency spike detected
  console.warn(`Order latency ${latency}ms exceeds threshold`);
  // Send to monitoring service (Datadog, Prometheus, etc.)
}

Limitations and trade-offs

  • WASM support: WASM modules require modern runtimes (Node.js 18+, modern browsers)
  • Cold starts: First signature after idle may be slower (use warm-up)
  • Memory: Persistent connections and caches increase memory usage
  • Testnet latency: Testnet may be slower than mainnet; benchmark on mainnet for production estimates

Next steps

On this page