The ultimate browser performance library for lightning-fast web experiences
Velocity.js is an intelligent performance optimization library that dramatically speeds up your website by predicting and prefetching resources before users need them. Using advanced caching strategies, user behavior analysis, and service worker technology, Velocity.js can reduce page load times by up to 90%.
- Predictive loading based on user behavior (hover, touch, scroll, visibility)
- Smart prioritization system with customizable weights
- Intersection Observer integration for visible link detection
- Subresource prefetching for critical CSS/JS files
- Multiple caching strategies: Cache First, Network First, Stale While Revalidate
- Service Worker integration with
velocity-worker.js
- IndexedDB persistence with LRU eviction (up to 100 cached resources)
- BroadcastChannel API for efficient cross-tab communication
- XSS protection with DOMPurify integration
- URL sanitization and validation
- Performance budgets to prevent resource exhaustion
- Background processing with Web Workers
- Usage pattern tracking for optimization insights
- Cache hit/miss metrics and performance monitoring
- Real-time statistics via developer console
- Automatic cleanup of stale resources
- Zero configuration - works out of the box
- Fully configurable for advanced use cases
- Visual feedback system (optional)
- Debug mode for development
- Include the library in your HTML:
<script src="velocity.min.js" type="application/javascript"></script>
- Add the velocity service worker script to your public directory:
public/
โโโ velocity.min.js
โโโ velocity-worker.js
โโโ index.html
- That's it! Velocity.js automatically initializes and starts optimizing your site.
// Initialize with custom configuration
const velocity = Velocity.init({
MAX_CACHED_LINKS: 200,
VISUAL_FEEDBACK: true,
DEBUG_MODE: true,
PRIORITY_WEIGHTS: {
click: 10,
hover: 6,
touch: 8,
visible: 4
}
});
// Get performance statistics
const stats = await velocity.getCacheStats();
console.log('Cache stats:', stats);
Metric | Before Velocity.js | After Velocity.js | Improvement |
---|---|---|---|
First Contentful Paint | 2.4s | 0.3s | 87% faster |
Largest Contentful Paint | 4.1s | 0.8s | 80% faster |
Time to Interactive | 5.2s | 1.1s | 79% faster |
Cache Hit Rate | 0% | 85% | โ improvement |
Results from real-world testing on a typical e-commerce site, your results may vary
const config = {
// Cache settings
MAX_CACHED_LINKS: 100, // Maximum cached resources
DB_NAME: 'VelocityCache', // IndexedDB database name
SW_PATH: '/velocity-worker.js', // Service worker path
// Performance settings
PREFETCH_TIMEOUT: 3000, // Prefetch timeout (ms)
MAX_CONCURRENT_PREFETCH: 3, // Max parallel prefetches
CLEANUP_INTERVAL: 300000, // Cleanup interval (ms)
// User experience
VISUAL_FEEDBACK: true, // Show loading indicators
DEBUG_MODE: false, // Enable debug logging
// Priority weights for different triggers
PRIORITY_WEIGHTS: {
click: 10, // Highest priority
touch: 7, // High priority (mobile)
hover: 5, // Medium priority
visible: 3 // Low priority (in viewport)
}
};
Velocity.init(config);
// Initialize with optional config
Velocity.init(config?: VelocityConfig): VelocityInstance
// Get current instance
Velocity.getInstance(): VelocityInstance | null
// Instance methods
instance.getCacheStats(): Promise<CacheStats>
instance.prefetchResources({url: string, priority: number, trigger: string}): Promise<void>
instance.clearCache(): Promise<void>
instance.invalidateCache(pattern: RegExp): Promise<void>
instance.updateConfig(newConfig: Partial<VelocityConfig>): void
instance.destroy(): void
// Listen for cache events
velocity.on('prefetch:start', (url) => {
console.log('Started prefetching:', url);
});
velocity.on('prefetch:complete', (url, success) => {
console.log('Prefetch completed:', url, success);
});
velocity.on('cache:hit', (url) => {
console.log('Cache hit for:', url);
});
-
๐ง Event Listening: Velocity.js listens for user interactions (mouse hover, touch start, clicks)
-
๐งฎ Smart Analysis: Analyzes user behavior patterns and calculates prefetch priorities
-
โก Intelligent Prefetching: Prefetches high-priority resources using multiple strategies:
- Native browser
<link rel="prefetch">
- Service Worker caching
- IndexedDB persistence
- Native browser
-
๐๏ธ Advanced Caching: Implements multiple caching strategies based on resource type:
- Static assets: Cache First
- HTML pages: Network First with cache fallback
- API requests: Network First with selective caching
- Prefetched content: Stale While Revalidate
-
๐งน Smart Cleanup: Automatically manages cache size using LRU (Least Recently Used) eviction
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Main Thread โโโโโบโ Service Worker โโโโโบโ IndexedDB โ
โ โ โ โ โ โ
โ โข Event capture โ โ โข Fetch control โ โ โข Persistence โ
โ โข Priority calc โ โ โข Cache strategyโ โ โข LRU eviction โ
โ โข User analyticsโ โ โข Background โ โ โข Analytics โ
โ โ โ prefetch โ โ โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโ
โ BroadcastChannelโ
โ โ
โ โข Real-time โ
โ communication โ
โ โข Performance โ
โ metrics โ
โโโโโโโโโโโโโโโโโโโ
Browser | Version | Service Worker | IndexedDB | BroadcastChannel |
---|---|---|---|---|
Chrome | 51+ | โ | โ | โ |
Firefox | 44+ | โ | โ | โ |
Safari | 11.1+ | โ | โ | โ |
Edge | 17+ | โ | โ | โ |
Velocity.js gracefully degrades in unsupported browsers
- XSS Protection: All cached content is sanitized using DOMPurify
- Same-Origin Policy: Only caches resources from the same origin
- URL Validation: Prevents malicious URL injection
- Content Sanitization: Strips dangerous scripts and event handlers
- HTTPS Ready: Optimized for secure connections
// Boost product page performance
Velocity.init({
PRIORITY_WEIGHTS: {
click: 15, // Product clicks are critical
hover: 8, // Product hovers are important
visible: 5 // Visible products get prefetched
},
MAX_CACHED_LINKS: 150 // More products = more cache
});
// Optimize article reading experience
Velocity.init({
PRIORITY_WEIGHTS: {
visible: 7, // Articles in viewport
hover: 4, // Article previews
click: 12 // Article opens
},
VISUAL_FEEDBACK: false // Clean reading experience
});
// Enhance route transitions
Velocity.init({
MAX_CONCURRENT_PREFETCH: 5, // More aggressive prefetching
PREFETCH_TIMEOUT: 5000, // Longer timeout for API calls
DEBUG_MODE: true // Monitor performance
});
Apache-2.0 license ยฉ Branislav Djalic
- Inspired by modern performance optimization techniques
- Built with โค๏ธ for the web development community
- ๐ค Buy me a coffee: https://coff.ee/omodaka9375
- ๐ Bug Reports: GitHub Issues
- ๐ฆ Twitter: @LordOfThePies4
Made with โก by developers, for developers
Star โญ this repo if Velocity.js helped speed up your website!