Performance Optimizer Examples

Real-world usage patterns and performance tracking demonstrations

Example 1: Basic Usage

This page already includes the performance optimizer! Check the <head> section to see how it's implemented.

Basic Implementation • HTML
<!DOCTYPE html>
<html>
<head>
    <!-- Load FIRST for maximum benefit -->
    <script src="javascript/performance-optimizer.js" data-track-metrics="true"></script>

    <!-- Your other resources -->
    <link rel="stylesheet" href="styles/grid.css">
    <script src="https://kit.fontawesome.com/your-kit.js"></script>
</head>

Example 2: View This Page's Metrics

Click the button below to see the actual performance metrics from loading this page:

Implementation Code • JavaScript
function showMetrics() {
    const metrics = RSL.PerformanceOptimizer.getMetrics();

    if (!metrics.pageLoadTime) {
        alert('Metrics not yet available. Wait for page to fully load.');
        return;
    }

    // Display formatted metrics
    console.log('Page Load Time:', metrics.pageLoadTime.toFixed(2) + 'ms');
    console.log('First Contentful Paint:', (metrics.fcp || 'N/A'));
    console.log('DOM Interactive:', metrics.domInteractive.toFixed(2) + 'ms');
}

Example 3: Custom Configuration

Configure the optimizer for your specific needs:

Custom Setup • HTML
<!-- Enable console logging for development -->
<script
  src="javascript/performance-optimizer.js"
  data-track-metrics="true"
  data-log-to-console="true">
</script>

<!-- Add custom preconnect domains -->
<script
  src="javascript/performance-optimizer.js"
  data-preconnect="https://cdn.example.com,https://api.example.com">
</script>

<!-- Disable lazy loading if needed -->
<script
  src="javascript/performance-optimizer.js"
  data-enable-lazy-loading="false">
</script>

Example 4: Tracking Performance Over Time

Send metrics to your analytics service:

Analytics Integration • JavaScript
// Wait for page to fully load
window.addEventListener('load', function() {
    setTimeout(function() {
        const metrics = RSL.PerformanceOptimizer.getMetrics();

        // Send to your analytics service
        fetch('/api/analytics/performance', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                page: window.location.pathname,
                metrics: {
                    pageLoadTime: metrics.pageLoadTime,
                    fcp: metrics.fcp,
                    domInteractive: metrics.domInteractive,
                    ttfb: metrics.ttfb
                },
                timestamp: new Date().toISOString()
            })
        });
    }, 100);
});

Example 5: A/B Testing Performance

Compare performance with and without the optimizer:

A/B Test Setup • JavaScript
// Randomly assign users to control or test group
const testGroup = Math.random() < 0.5 ? 'control' : 'optimized';

// Only load optimizer for test group
if (testGroup === 'optimized') {
    const script = document.createElement('script');
    script.src = 'javascript/performance-optimizer.js';
    script.setAttribute('data-track-metrics', 'true');
    document.head.insertBefore(script, document.head.firstChild);
}

// Track results
window.addEventListener('load', function() {
    setTimeout(function() {
        const perfData = performance.getEntriesByType('navigation')[0];
        const loadTime = perfData.loadEventEnd - perfData.fetchStart;

        // Log to analytics
        console.log('Group:', testGroup);
        console.log('Load Time:', loadTime.toFixed(2) + 'ms');

        // Send to analytics service
        // ...
    }, 100);
});