Registry / web-framework / metal-throttle

metal-throttle

JSON →
library3.0.1jsnpmunverified

The `metal-throttle` package provides a utility function to limit the rate at which a function can be called. This is essential for optimizing performance in web applications by preventing excessive execution of handlers for frequent events like scrolling, resizing, or rapid clicks. It ensures a function executes at most once within a specified time window, discarding subsequent calls until the timeout expires. As part of the broader Metal.js ecosystem, it aims to offer a reliable and efficient throttling mechanism. Currently at version `3.0.1`, it is a stable utility. Without more specific release information, its cadence is generally aligned with the Metal.js project. Key differentiators typically revolve around its integration within the Metal.js framework, potentially offering optimizations or API consistency tailored for that environment, though basic throttling logic is common across many libraries.

npm install metal-throttle
INSTALL
IMPORT
SIG · METAL-THROTTLE
M
metal-throttle
web-frameworkjavascriptv3.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

throttle
import { throttle } from 'metal-throttle';
const throttle = require('metal-throttle');
For modern JavaScript environments (ESM), use named import. CommonJS `require` might return an object, requiring `.throttle` access.
throttle
const { throttle } = require('metal-throttle');
import throttle from 'metal-throttle';
For CommonJS environments, use destructuring assignment from `require()`. A default import is not typically provided for this utility.

Demonstrates how to import and use the `throttle` function with both scroll and click events to limit their execution rate over time.

import { throttle } from 'metal-throttle'; // Example 1: Throttling a scroll event handler function handleScroll() { const scrollPosition = window.scrollY; console.log(`Scrolling: Current position is ${scrollPosition}px at ${Date.now()}`); // In a real app, you might update UI based on scroll position } // Throttled version of handleScroll, runs at most once every 200ms const throttledScrollHandler = throttle(handleScroll, 200); // Attach the throttled handler to the scroll event window.addEventListener('scroll', throttledScrollHandler); console.log("Scroll down rapidly to see the effect of throttling in the console (output will be limited)."); // Example 2: Throttling a button click handler let clickCount = 0; const button = document.createElement('button'); button.textContent = 'Click me (throttled)'; document.body.appendChild(button); function handleClick() { clickCount++; console.log(`Button clicked! Total clicks: ${clickCount} at ${Date.now()}`); } // Throttled version of handleClick, runs at most once every 1000ms (1 second) const throttledClickHandler = throttle(handleClick, 1000); button.addEventListener('click', throttledClickHandler); console.log("A button has been added to the page. Click it rapidly to observe how the console output is throttled."); // Cleanup (optional, for demonstration purposes) setTimeout(() => { window.removeEventListener('scroll', throttledScrollHandler); button.removeEventListener('click', throttledClickHandler); button.remove(); console.log("Demonstration complete. Event listeners removed."); }, 10000);
Debug
Known issues
breakingMajor versions (like 3.0.0) typically introduce breaking changes. Always review the release notes or changelog when upgrading from an earlier major version to ensure compatibility with API changes, argument order, or return values.
fix
Consult the `metal-throttle` v3 migration guide or changelog. Update function signatures and usage patterns according to the new API specification.
affects: >=3.0.0
gotchaThe `this` context inside a throttled function can be lost if not explicitly bound or managed. This is a common JavaScript pitfall, not specific to `metal-throttle`, but important when working with class methods or object methods.
fix
Use an arrow function (if defining the throttled function inline) or explicitly `bind` the function: `throttle(myMethod.bind(this), delay)`.
affects: >=0.12.0
gotchaWhen using `throttle` with event listeners, ensure that you store a reference to the *throttled* function to correctly remove the event listener later. Removing `window.removeEventListener('scroll', handleScroll);` will not work if `handleScroll` was attached via `window.addEventListener('scroll', throttledHandleScroll);`.
fix
Always use the same throttled function reference for both `addEventListener` and `removeEventListener`: `const throttledFn = throttle(myFn, delay); element.addEventListener('event', throttledFn); element.removeEventListener('event', throttledFn);`
affects: >=0.12.0
Errors
Common errors & fixes
TypeError: (0 , _metalThrottle.throttle) is not a function
Incorrect import statement, often when mixing CommonJS `require` with ESM `import` syntax or attempting to use a default import for a named export.
fix
For ESM, use `import { throttle } from 'metal-throttle';`. For CommonJS, use `const { throttle } = require('metal-throttle');`.
throttle is not defined
The `throttle` function was used without being imported or correctly made available in the current scope.
fix
Add the correct import statement at the top of your file: `import { throttle } from 'metal-throttle';` or `const { throttle } = require('metal-throttle');`.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
metal-throttle — npm install metal-throttle · libregistry