Registry / http-networking / throttle-typescript

throttle-typescript

JSON →
library1.1.0jsnpmunverified

The `throttle-typescript` package provides a lightweight, TypeScript-first utility for function throttling. It enables developers to limit the rate at which a given function can be invoked, which is crucial for performance optimization in applications dealing with high-frequency events like window resizing, scrolling, or user input. The current stable version is 1.1.0, featuring an updated return value mechanism that now provides the result of the last successful function execution, alongside refined TypeScript type definitions. The library's core differentiator is its native inclusion of TypeScript types, eliminating the need for separate `@types/` packages and ensuring a smooth development experience within TypeScript projects. While the release cadence appears stable rather than rapid, it indicates a focus on robustness and type correctness. This package is ideal for scenarios where continuous event handling needs to be managed efficiently to prevent UI jank and improve overall responsiveness. It contrasts with debouncing by ensuring periodic execution rather than waiting for an idle period.

npm install throttle-typescript
INSTALL
IMPORT
SIG · THROTTLE-TYPESCRIP
T
throttle-typescript
http-networkingjavascriptv1.1.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

throttle
✓ import { throttle } from 'throttle-typescript';
✗ const throttle = require('throttle-typescript');
The package is primarily designed for ES Modules and ships with TypeScript types. While CommonJS `require` might work in some environments, ES module import is the recommended approach for modern JavaScript and TypeScript.

Demonstrates how to install `throttle-typescript` and use the `throttle` function to limit the execution rate of an event handler (e.g., `window.resize`) and direct function calls, showcasing its behavior.

import { throttle } from 'throttle-typescript'; // A function that simulates some expensive operation, like a complex UI update let executionCount = 0; function handleExpensiveOperation() { executionCount++; const timestamp = new Date().toLocaleTimeString(); console.log(`[${timestamp}] Heavy operation executed! Count: ${executionCount}`); // In a real application, this might involve DOM manipulation, recalculations, network requests, etc. } // Create a throttled version of the function // It will run at most once every 200 milliseconds. const throttledHandler = throttle(handleExpensiveOperation, 200); // Attach the throttled function to a common high-frequency event, like window resize window.addEventListener('resize', throttledHandler); // You can also call it directly, and it will respect the throttle limit let intervalCalls = 0; const intervalId = setInterval(() => { if (intervalCalls >= 10) { clearInterval(intervalId); console.log('Stopped interval calls.'); return; } console.log(`Calling throttledHandler from interval (call ${intervalCalls + 1})...`); throttledHandler(); intervalCalls++; }, 50); console.log("Setup complete. Try resizing your browser window or observe the interval calls in the console.");
Debug
Known issues
gotchaThe return value of the throttled function changed in v1.1.0. It now reflects the result of the last actual execution of the underlying function. Previously, the return value behavior was less explicitly defined.
fix
If your code relies on the return value of the throttled function, ensure it correctly handles this new behavior. Verify that any assignments or conditional logic based on the throttled function's output are still valid.
affects: >=1.1.0
gotchaWhen throttling an object method (e.g., `this.myMethod`), the `this` context can be lost within the throttled function, leading to `TypeError: Cannot read properties of undefined`.
fix
Preserve the `this` context by binding the method or using an arrow function: `throttle(this.myMethod.bind(this), 100)` or `throttle(() => this.myMethod(), 100)`.
affects: >=1.0.0
gotchaThis package implements 'throttling', which ensures a function executes at most once per a specified time interval. It is not 'debouncing', which executes a function only after a period of inactivity.
fix
Confirm that throttling is the correct pattern for your use case. If you need to respond to an event only once activity has ceased (e.g., searching after a user stops typing), you should use a 'debounce' utility instead.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'myMethod')
The `this` context was lost when throttling an object method without binding it.
fix
Bind the method to its `this` context: `window.addEventListener('scroll', throttle(this.myMethod.bind(this), 100));` or use an arrow function: `window.addEventListener('scroll', throttle(() => this.myMethod(), 100));`
Module '"throttle-typescript"' has no exported member 'default'.
Attempting to import the `throttle` function as a default import when it is a named export.
fix
Use a named import: `import { throttle } from 'throttle-typescript';`
Cannot find module 'throttle-typescript' or its corresponding type declarations.
The package has not been installed, or there is a typo in the import path. For TypeScript, it can also indicate an issue with `tsconfig.json` or module resolution.
fix
Ensure the package is installed: `npm install throttle-typescript` or `yarn add throttle-typescript`. Double-check the import statement for typos: `import { throttle } from 'throttle-typescript';`
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
throttle-typescript — npm install throttle-typescript · libregistry