UNPKG

snpk-cycle

Version:

> High Precision Adaptive Timer for Node.js

54 lines (42 loc) โ€ข 1.58 kB
# Node-Cycle > High Precision Adaptive Timer for Node.js > [!TIP] > How to bypass event loop delays? > The loop itself bypasses and takes into account event loop delays, but I highly recommend using `custom.step` and dynamically changing the loop time there. ## ๐Ÿงช Tested For - โœ… Discord UDP/RTP VoIP streams - โœ… Real-world drift: `~0.05ms` - โœ… FFmpeg filter loops (EQ, tempo, pitch, etc.) --- ## ๐Ÿš€ Features - ๐Ÿ“ **Sub-millisecond drift correction** using `performance.now()` + `Date.now` - ๐Ÿ” **Stable timing** even under event loop pressure - ๐ŸŽฏ **Self-correcting loop** with zero-delay recovery - ๐Ÿง  **No native bindings**, 100% JavaScript - โ™ป๏ธ **Auto garbage collection** after cycle completes - ๐Ÿงฉ **Extremely modular** โ€” plug into your own system (`TaskCycle`) --- ### How to use ```ts import { TaskCycle } from 'snpk-cycle'; const cycles = new class LowerCycle<T extends any> extends TaskCycle<T> { public constructor() { super({ // Time until next cycle run duration: 20, // If dot not need drifting in cycle drift: false, // Custom functions (if you want to change the execution logic a little) custom: {}, // Check function filter: (item: T) => true, // Execution of the function after the specified time, taking into account the filter execute: (item: T) => { // Your code } }); }; }; // Adding your object cycles.add(item); ```