Skip to content

Commit d7949fb

Browse files
add performance utilities; implement debounce and throttle functions for improved function execution control
1 parent 74e01c6 commit d7949fb

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./browser";
22
export * from "./utils";
3+
export * from "./performance";

src/performance/debounce/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @fileoverview Debounce utility — delays function execution until after a pause.
3+
* Useful for input events, search bars, resize listeners, etc.
4+
*/
5+
6+
type DebouncedFunction<T extends (...args: any[]) => void> = (...args: Parameters<T>) => void;
7+
8+
/**
9+
* Creates a debounced version of the given function.
10+
* @param fn - The function to debounce
11+
* @param delay - Delay in milliseconds
12+
* @returns A debounced function
13+
*/
14+
export function debounce<T extends (...args: any[]) => void>(
15+
fn: T,
16+
delay: number
17+
): DebouncedFunction<T> {
18+
if (typeof fn !== 'function') {
19+
throw new TypeError('[webdev-power-kit][debounce] First argument must be a function.');
20+
}
21+
22+
if (typeof delay !== 'number' || delay < 0) {
23+
throw new TypeError('[webdev-power-kit][debounce] Delay must be a non-negative number.');
24+
}
25+
26+
let timer: ReturnType<typeof setTimeout> | null = null;
27+
28+
return function debouncedFn(...args: Parameters<T>) {
29+
if (timer) clearTimeout(timer);
30+
31+
timer = setTimeout(() => {
32+
try {
33+
fn(...args);
34+
} catch (err) {
35+
console.error('[webdev-power-kit][debounce] Function threw an error:', err);
36+
}
37+
}, delay);
38+
};
39+
}

src/performance/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./debounce";
2+
export * from "./throttle";

src/performance/throttle/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @fileoverview Throttle utility — limits how often a function can run.
3+
* Useful for scroll events, mouse moves, resize, etc.
4+
*/
5+
6+
type ThrottledFunction<T extends (...args: any[]) => void> = (...args: Parameters<T>) => void;
7+
8+
/**
9+
* Creates a throttled version of the given function.
10+
* @param fn - Function to throttle
11+
* @param interval - Minimum time (in ms) between calls
12+
* @returns A throttled function
13+
*/
14+
export function throttle<T extends (...args: any[]) => void>(
15+
fn: T,
16+
interval: number
17+
): ThrottledFunction<T> {
18+
if (typeof fn !== 'function') {
19+
throw new TypeError('[webdev-power-kit][throttle] First argument must be a function.');
20+
}
21+
22+
if (typeof interval !== 'number' || interval < 0) {
23+
throw new TypeError('[webdev-power-kit][throttle] Interval must be a non-negative number.');
24+
}
25+
26+
let lastCalled = 0;
27+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
28+
29+
return function throttledFn(...args: Parameters<T>) {
30+
const now = Date.now();
31+
const timeSinceLastCall = now - lastCalled;
32+
33+
if (timeSinceLastCall >= interval) {
34+
lastCalled = now;
35+
try {
36+
fn(...args);
37+
} catch (err) {
38+
console.error('[webdev-power-kit][throttle] Function threw an error:', err);
39+
}
40+
} else if (!timeoutId) {
41+
const timeRemaining = interval - timeSinceLastCall;
42+
timeoutId = setTimeout(() => {
43+
timeoutId = null;
44+
lastCalled = Date.now();
45+
try {
46+
fn(...args);
47+
} catch (err) {
48+
console.error('[webdev-power-kit][throttle] Function threw an error:', err);
49+
}
50+
}, timeRemaining);
51+
}
52+
};
53+
}

0 commit comments

Comments
 (0)