Skip to content

Performance Guide

Comprehensive guide to optimizing Movian plugin and UI performance.

Overview

Performance optimization focuses on: - Fast loading times - Smooth UI rendering - Efficient memory usage - Responsive interactions - Network efficiency

Plugin Performance

Async Operations

// GOOD: Non-blocking
page.loading = true;
http.request(url, function(err, response) {
    page.loading = false;
    processData(response);
});

// BAD: Blocks execution
var response = http.request(url);

Caching

var cache = {};

function getCachedData(key) {
    if (cache[key]) {
        return cache[key];
    }

    var data = fetchData(key);
    cache[key] = data;
    return data;
}

Lazy Loading

function loadPage(offset, limit) {
    http.request(url + '?offset=' + offset + '&limit=' + limit,
        function(err, response) {
            displayItems(response);

            if (hasMore) {
                loadPage(offset + limit, limit);
            }
        });
}

UI Performance

Minimize Updates

// BAD: Multiple updates
page.metadata.title = "Title";
page.metadata.icon = "icon.png";
page.metadata.background = "bg.jpg";

// GOOD: Batch updates
var meta = page.metadata;
meta.title = "Title";
meta.icon = "icon.png";
meta.background = "bg.jpg";

Efficient Rendering

// Use hidden instead of conditional loading
widget(container_x, {
    hidden: !$condition;  // Fast toggle
});

// Avoid excessive nesting
// Keep widget hierarchy shallow

Database Performance

Use Indexes

db.exec("CREATE INDEX IF NOT EXISTS idx_timestamp ON cache(timestamp)");

Prepared Statements

var stmt = db.prepare("SELECT * FROM users WHERE id = ?");
for (var i = 0; i < 1000; i++) {
    stmt.bind(i).execute();
}
stmt.finalize();

Transactions

db.begin();
// Multiple operations
db.commit();

Network Performance

Request Throttling

var activeRequests = 0;
var MAX_CONCURRENT = 4;

function throttledRequest(url, callback) {
    if (activeRequests >= MAX_CONCURRENT) {
        setTimeout(function() {
            throttledRequest(url, callback);
        }, 100);
        return;
    }

    activeRequests++;
    http.request(url, function(err, response) {
        activeRequests--;
        callback(err, response);
    });
}

Compression

http.request(url, {
    compression: true
});

Memory Management

Limit Cache Size

var MAX_CACHE_SIZE = 100;

if (Object.keys(cache).length > MAX_CACHE_SIZE) {
    // Remove oldest entries
    var oldest = Object.keys(cache)[0];
    delete cache[oldest];
}

Clean Up Resources

page.onDestroy(function() {
    subscriptions.forEach(function(sub) {
        sub.destroy();
    });
});

Profiling

Timing

var start = Date.now();
performOperation();
console.log("Took " + (Date.now() - start) + "ms");

Memory Usage

function getCacheSize() {
    return JSON.stringify(cache).length;
}

Best Practices

  • Use async operations
  • Cache frequently accessed data
  • Minimize UI updates
  • Use database indexes
  • Throttle network requests
  • Clean up resources
  • Profile performance bottlenecks

See Also