throttle-debounce rate limiting: working versions (tested 2026-10-16)
Wrap a counter with throttle-debounce debounce and throttle, asserting debounce collapses rapid calls and throttle limits rate using node:test fake timers, including cancel.
passed3 passed · 0 failed2026-10-16 02:18 UTC- Status
- passed
- Last passed
- 2026-10-16 02:18 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
throttle-debounce@5.0.2@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38839686104
The code
Exactly the files that passed. Your agent can pull them with get_recipe("throttle-debounce-fns") (install), or see them on GitHub.
package.json
{
"name": "throttle-debounce-fns",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"throttle-debounce": "5.0.2"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/counter.ts
import { debounce, throttle } from "throttle-debounce";
export interface Counter {
increment(): number;
readonly value: number;
}
export function createCounter(): Counter {
let count = 0;
return {
increment() {
count += 1;
return count;
},
get value() {
return count;
},
};
}
export interface Cancelable {
cancel(): void;
}
export type DebouncedIncrement = (() => void) & Cancelable;
export type ThrottledIncrement = (() => void) & Cancelable;
export function debouncedIncrement(
delayMs: number,
counter: Counter,
): DebouncedIncrement {
return debounce(delayMs, () => {
counter.increment();
}) as DebouncedIncrement;
}
export function throttledIncrement(
delayMs: number,
counter: Counter,
): ThrottledIncrement {
return throttle(delayMs, () => {
counter.increment();
}) as ThrottledIncrement;
}
test/counter.test.ts
import { test, mock } from "node:test";
import assert from "node:assert/strict";
import {
createCounter,
debouncedIncrement,
throttledIncrement,
} from "../src/counter.ts";
function withFakeTimers(run: () => void): void {
mock.timers.enable({ apis: ["setTimeout", "Date"], now: 1_000_000 });
try {
run();
} finally {
mock.timers.reset();
}
}
test("debounce collapses rapid calls into a single execution", () => {
withFakeTimers(() => {
const counter = createCounter();
const bump = debouncedIncrement(100, counter);
bump();
bump();
bump();
bump();
assert.equal(counter.value, 0);
mock.timers.tick(100);
assert.equal(counter.value, 1);
});
});
test("debounce cancel drops the pending execution", () => {
withFakeTimers(() => {
const counter = createCounter();
const bump = debouncedIncrement(100, counter);
bump();
assert.equal(typeof bump.cancel, "function");
bump.cancel();
mock.timers.tick(500);
assert.equal(counter.value, 0);
});
});
test("throttle limits the rate of calls", () => {
withFakeTimers(() => {
const counter = createCounter();
const bump = throttledIncrement(100, counter);
bump();
bump();
bump();
assert.equal(counter.value, 1);
mock.timers.tick(100);
assert.equal(counter.value, 2);
});
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.