Lightweight script to improve page load speeds through resource hints, progressive loading, and smart initialization
Add to the top of your <head> section for maximum benefit:
<!DOCTYPE html>
<html>
<head>
<!-- Performance Optimizer - Load first -->
<script src="javascript/performance-optimizer.js"></script>
<!-- Rest of your CSS and JS files -->
<link rel="stylesheet" href="styles/grid.css">
</head>
The script automatically optimizes your page. No configuration required.
Establishes early connections to external CDNs (FontAwesome, Google Fonts) before resources are requested.
Impact: Saves 100-300ms on external resource loading
Tells the browser to prioritize loading grid.css, modern-theme.css
, and layout.js.
Impact: Improves First Contentful Paint by 200-400ms
Automatically adds loading="lazy" to images and iframes not in the viewport.
Impact: Reduces initial page weight by 40-60%
Adds font-display: swap to prevent invisible text while fonts load.
Impact: Eliminates FOIT (Flash of Invisible Text)
Collects detailed performance data using the Navigation Timing API.
Metrics tracked: DNS, TCP, TTFB, DOM Interactive, Page Load Time, First Contentful Paint
Configure via data attributes on the script tag:
<script
src="javascript/performance-optimizer.js"
data-preconnect="https://kit.fontawesome.com,https://fonts.googleapis.com"
data-track-metrics="true"
data-log-to-console="true"
data-enable-lazy-loading="true">
</script>
| Attribute | Type | Default | Description |
|---|---|---|---|
data-preconnect |
String | FontAwesome, Google Fonts | Comma-separated list of domains to preconnect |
data-track-metrics |
Boolean | true | Enable/disable performance tracking |
data-log-to-console |
Boolean | false | Automatically log metrics to console on page load |
data-enable-lazy-loading |
Boolean | true | Enable native lazy loading for images/iframes |
// Wait for page load
window.addEventListener('load', function() {
setTimeout(function() {
const metrics = RSL.PerformanceOptimizer.getMetrics();
console.log('Page Load Time:', metrics.pageLoadTime + 'ms');
console.log('First Contentful Paint:', metrics.fcp + 'ms');
}, 100);
});
// Manually log metrics after page load
RSL.PerformanceOptimizer.logMetrics();
| Method | Returns | Description |
|---|---|---|
getMetrics() |
Object | Returns performance metrics object with timing data |
logMetrics() |
void | Logs formatted metrics to browser console |
isEnabled() |
Boolean | Check if optimizer is running (always true) |
getConfig() |
Object | Returns current configuration settings |
The metrics object returned by getMetrics() contains:
| Property | Type | Description |
|---|---|---|
pageLoadTime |
Number | Total time from fetch start to load complete (ms) |
fcp |
Number | First Contentful Paint - when first content renders (ms) |
dns |
Number | DNS lookup time (ms) |
tcp |
Number | TCP connection time (ms) |
ttfb |
Number | Time to First Byte - server response time (ms) |
download |
Number | Resource download time (ms) |
domInteractive |
Number | When DOM is interactive (ms) |
domComplete |
Number | When DOM is fully loaded (ms) |
domContentLoaded |
Number | DOMContentLoaded event duration (ms) |
Use the optimizer's built-in tracking:
// In browser console after page loads:
RSL.PerformanceOptimizer.logMetrics();
// Output:
// Page Load Time: 1234.56ms
// DOM Interactive: 567.89ms
// First Contentful Paint: 345.67ms
// ...
<head> for maximum benefit. Resource hints work best when added before any resources are requested.
Supports all modern browsers:
Graceful degradation: Older browsers ignore unsupported features without errors.