UNPKG

snowflake-sql-validator

Version:

Snowflake SQL validator for React applications

113 lines 3.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PerformanceOptimizer = void 0; /** * Performance optimization utilities for the Snowflake SQL Validator */ class PerformanceOptimizer { /** * Get cached value with LRU eviction */ static getCached(key) { if (this.lruCache.has(key)) { // Update access count const count = this.CACHE_ACCESS_COUNT.get(key) || 0; this.CACHE_ACCESS_COUNT.set(key, count + 1); return this.lruCache.get(key); } return undefined; } /** * Set cached value with LRU eviction */ static setCached(key, value) { // Evict least recently used items if cache is full if (this.lruCache.size >= this.MAX_CACHE_SIZE) { this.evictLRU(); } this.lruCache.set(key, value); this.CACHE_ACCESS_COUNT.set(key, 1); } /** * Evict least recently used items from cache */ static evictLRU() { let minKey = ''; let minCount = Infinity; for (const [key, count] of this.CACHE_ACCESS_COUNT.entries()) { if (count < minCount) { minCount = count; minKey = key; } } if (minKey) { this.lruCache.delete(minKey); this.CACHE_ACCESS_COUNT.delete(minKey); } } /** * Clear all caches */ static clearAllCaches() { this.lruCache.clear(); this.CACHE_ACCESS_COUNT.clear(); } /** * Get cache statistics */ static getCacheStats() { return { size: this.lruCache.size, maxSize: this.MAX_CACHE_SIZE, hitRate: 0 // TODO: Implement hit rate calculation }; } /** * Debounce function calls for performance */ static debounce(func, wait) { let timeout; return (...args) => { clearTimeout(timeout); timeout = setTimeout(() => func(...args), wait); }; } /** * Throttle function calls for performance */ static throttle(func, limit) { let inThrottle; return (...args) => { if (!inThrottle) { func(...args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } }; } /** * Batch multiple operations for better performance */ static batch(items, batchSize, processor) { for (let i = 0; i < items.length; i += batchSize) { const batch = items.slice(i, i + batchSize); processor(batch); } } /** * Measure execution time of a function */ static measureTime(fn, label) { const start = performance.now(); const result = fn(); const end = performance.now(); console.log(`${label} took ${end - start}ms`); return result; } } exports.PerformanceOptimizer = PerformanceOptimizer; // LRU Cache implementation for better memory management PerformanceOptimizer.lruCache = new Map(); PerformanceOptimizer.MAX_CACHE_SIZE = 500; PerformanceOptimizer.CACHE_ACCESS_COUNT = new Map(); //# sourceMappingURL=PerformanceOptimizer.js.map