Skip to content

High-precision absolute-time timer for Node.js on Windows using Timer Queue

License

Notifications You must be signed in to change notification settings

00nx/node-epoch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-epoch

High-precision absolute epoch timer for Node.js on Windows
Ultra-low jitter timer using Windows Timer Queue — perfect for trading, and real-time applications where setTimeout is too slow or drifts too much.

Current typical jitter: 1–10 ms (often better than native setTimeout under load)

Windows only — uses CreateTimerQueueTimer + ThreadSafeFunction

Features

  • Trigger callbacks at absolute epoch timestamps (not relative delays)
  • Non-blocking (uses Windows thread pool)
  • Supports units: seconds, milliseconds, microseconds, nanoseconds
  • Very low overhead compared to busy-wait approaches
  • Designed for latency-sensitive use-cases

Installation

# Recommended: from GitHub (latest version)
npm install github:00nx/node-epoch

quickstart

const { setEpochTimer } = require('node-epoch');

console.log("Arming precise epoch timers...");

// 1. Fire exactly 5 seconds from now (seconds precision)
setEpochTimer("s", Math.floor(Date.now() / 1000) + 5, () => {
  console.log("→ 5-second epoch timer fired!", new Date().toISOString());
});

// 2. Most common: millisecond precision
setEpochTimer("ms", Date.now() + 3500, () => {
  console.log("→ 3.5 second precise timer!", new Date().toISOString());
});

// 3. Warmup + ultra-low latency claim example (vanity/discord style)
const now = Date.now();
setEpochTimer("ms", now + 5000, () => {
  console.log("[warmup] 5s warmup complete");
  setEpochTimer("ms", now + 5500, () => {
    console.log("[claim] ULTRA LOW LATENCY CLAIM FIRED", new Date().toISOString());
  });
});

api

setEpochTimer(
  unit: "s" | "ms" | "us" | "ns",
  value: number,
  callback: () => void
): void
  • unit — time unit of the target timestamp
  • value — absolute Unix timestamp in the given unit
  • callback — function to execute when the target time is reached

If the target time is in the past or now → callback runs immediately (next tick).

Build from source

git clone https://github.com/00nx/node-epoch.git
cd node-epoch
npm install
npm run build

Why this over setTimeout?

  • Better accuracy for absolute timestamps
  • Less drift under system load
  • Designed for trigger patterns where 10–50 ms can make a difference