{"library":"retimer","title":"Reschedulable Timer","description":"Retimer is a JavaScript library providing a reschedulable alternative to Node.js's native `setTimeout`. It is specifically designed for scenarios requiring frequent timer rescheduling, such as managing keep-alive functionality across numerous client connections or sockets, where repeatedly clearing and setting `setTimeout` can become a performance bottleneck due to Node.js's internal timer linked list management. The library works by allowing old timers to run out naturally while scheduling new ones, optimizing performance when rescheduling a timeout *after* its original duration. The current stable version is 4.0.0, which notably introduced TypeScript types and support for `worker-timers`. While release cadence isn't strictly defined, major versions appear every few years, indicating a mature and stable project. Its key differentiator is the performance optimization for 'reschedule after' scenarios, aiming for better efficiency than naive `clearTimeout`/`setTimeout` cycles.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install retimer"],"cli":null},"imports":["import retimer from 'retimer';","import retimer, { Retimer } from 'retimer';","const retimer = require('retimer');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import retimer from 'retimer';\n\nfunction setupTimer(initialTimeoutMs: number) {\n  const timerCallback = () => {\n    console.log('Timer executed after reschedule.');\n    // In a real application, you might re-evaluate what to do next\n    // or simply let the timer be cleared if it's a one-off.\n  };\n\n  const timer = retimer(timerCallback, initialTimeoutMs);\n  console.log(`Initial timer set for ${initialTimeoutMs}ms.`);\n\n  setTimeout(() => {\n    const newTimeoutMs = 50;\n    timer.reschedule(newTimeoutMs);\n    console.log(`Timer rescheduled for ${newTimeoutMs}ms.`);\n\n    setTimeout(() => {\n      console.log('Clearing timer.');\n      timer.clear();\n    }, newTimeoutMs + 10); // Clear after the rescheduled timeout plus a small buffer\n\n  }, initialTimeoutMs / 2); // Reschedule halfway through original timeout\n}\n\n// Example usage:\nsetupTimer(100);\n","lang":"typescript","description":"Demonstrates initializing a retimer, rescheduling it, and finally clearing it, showcasing the basic API flow for managing reschedulable timeouts.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}