48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { clsx, type ClassValue } from "clsx"
|
|
import { twMerge } from "tailwind-merge"
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs))
|
|
}
|
|
|
|
// Cache de performance
|
|
const perfCache = new Map<string, number>()
|
|
|
|
// Debounce utility
|
|
export function debounce<T extends (...args: any[]) => any>(
|
|
func: T,
|
|
wait: number
|
|
): (...args: Parameters<T>) => void {
|
|
let timeout: NodeJS.Timeout
|
|
|
|
return (...args: Parameters<T>) => {
|
|
clearTimeout(timeout)
|
|
timeout = setTimeout(() => func(...args), wait)
|
|
}
|
|
}
|
|
|
|
// Memoização para cálculos pesados
|
|
export function memoize<T extends (...args: any[]) => any>(
|
|
func: T,
|
|
resolver?: (...args: Parameters<T>) => string
|
|
): T {
|
|
const cache = new Map<string, ReturnType<T>>()
|
|
|
|
return ((...args: Parameters<T>) => {
|
|
const key = resolver ? resolver(...args) : JSON.stringify(args)
|
|
if (cache.has(key)) {
|
|
return cache.get(key)
|
|
}
|
|
const result = func(...args)
|
|
cache.set(key, result)
|
|
return result
|
|
}) as T
|
|
}
|
|
|
|
// Medição de performance
|
|
export function measurePerformance(key: string, callback: () => void) {
|
|
const start = performance.now()
|
|
callback()
|
|
const end = performance.now()
|
|
perfCache.set(key, end - start)
|
|
} |