Skip to content

MohdYahyaMahmodi/hundredmillioncheckboxes

Repository files navigation

One Hundred Million Checkboxes

Introduction

One Hundred Million Checkboxes is a highly optimized, real-time collaborative web application that allows users to interact with an enormous grid of 100 million checkboxes. This project demonstrates advanced techniques in web development, focusing on performance optimization, efficient data management, and real-time synchronization across multiple clients.

Technical Overview

Architecture

The application is built using a client-server architecture:

  • Frontend: HTML, CSS, and JavaScript
  • Backend: Node.js with Express.js
  • Real-time Communication: Socket.IO

Key Features

  1. Rendering 100 million checkboxes efficiently
  2. Real-time synchronization across multiple clients
  3. Optimized scrolling and viewport rendering
  4. Chunked data transfer between client and server
  5. Responsive design for various screen sizes
  6. Integrated chat functionality

How It Works

Checkbox Rendering and Management

Virtual DOM and Windowing Technique

To handle 100 million checkboxes efficiently, the application employs a virtual DOM approach combined with a windowing technique:

  1. Grid Calculation: The total grid size is calculated based on the viewport dimensions and checkbox size.
  2. Viewport Rendering: Only the checkboxes visible in the current viewport (plus a small buffer) are actually rendered in the DOM.
  3. Scroll Optimization: As the user scrolls, the visible checkboxes are dynamically updated, removing out-of-view checkboxes and adding newly visible ones.
function updateVisibleRows() {
    const scrollTop = checkboxContainer.scrollTop;
    visibleStartRow = Math.floor(scrollTop / TOTAL_CHECKBOX_SIZE) - VIEWPORT_BUFFER;
    visibleStartRow = Math.max(0, visibleStartRow);
    const viewportHeight = checkboxContainer.clientHeight;
    const visibleRows = Math.ceil(viewportHeight / TOTAL_CHECKBOX_SIZE) + 2 * VIEWPORT_BUFFER;
    visibleEndRow = Math.min(visibleStartRow + visibleRows, totalRows - 1);

    renderVisibleCheckboxes();
}

Chunked Rendering

To prevent browser freezing when rendering large numbers of checkboxes, the rendering process is chunked and uses requestAnimationFrame:

function renderChunk() {
    const chunkEndIndex = Math.min(currentIndex + RENDER_CHUNK_SIZE, endIndex);
    for (let i = currentIndex; i < chunkEndIndex; i++) {
        if (!document.getElementById(`cb-${i}`)) {
            fragment.appendChild(createCheckboxElement(i));
        }
    }
    checkboxGrid.appendChild(fragment);
    currentIndex = chunkEndIndex;

    if (currentIndex < endIndex) {
        requestAnimationFrame(renderChunk);
    }
}

Data Management and Synchronization

Server-Side Data Structure

The server uses a Set to efficiently store the indices of checked boxes:

const checkedBoxes = new Set(); // Store checked box indices

This allows for O(1) time complexity for adding, removing, and checking the state of any checkbox.

Chunked Data Transfer

To minimize data transfer and improve performance, the application uses a chunking strategy:

  1. The client requests data for visible chunks only.
  2. The server sends chunk data containing only the indices of checked boxes within that chunk.
socket.on('request checkbox chunk', (chunkIndex) => {
    const start = chunkIndex * CHUNK_SIZE;
    const end = Math.min((chunkIndex + 1) * CHUNK_SIZE, TOTAL_CHECKBOXES);
    const chunkCheckedBoxes = Array.from(checkedBoxes).filter(index => index >= start && index < end);
    socket.emit('checkbox chunk', { chunkIndex, checkedBoxes: chunkCheckedBoxes });
});

Real-time Updates

Socket.IO is used for real-time communication between the client and server:

  1. When a user checks or unchecks a box, an update is sent to the server.
  2. The server broadcasts this update to all connected clients.
  3. Clients update their local state and UI accordingly.
socket.on('checkbox update', ({ index, checked, totalChecked }) => {
    if (checked) {
        checkedBoxes.add(index);
    } else {
        checkedBoxes.delete(index);
    }
    updateCheckedCount(totalChecked);
    
    const checkbox = document.getElementById(`cb-${index}`);
    if (checkbox) {
        checkbox.checked = checked;
    }
});

Performance Optimizations

  1. Efficient DOM Manipulation: Using document.createDocumentFragment() for batch DOM updates.
  2. Debounced Scroll Handling: Using requestAnimationFrame to optimize scroll performance.
  3. Lazy Loading: Only loading and rendering checkboxes as they become visible.
  4. Minimized Data Transfer: Sending only necessary data (checked box indices) between client and server.

Additional Features

Chat Functionality

The application includes a real-time chat feature, allowing users to communicate while interacting with the checkboxes. This demonstrates the versatility of the Socket.IO implementation.

Responsive Design

The layout adapts to different screen sizes, ensuring a consistent experience across devices.

Setup and Deployment

  1. Clone the repository
  2. Install dependencies: npm install
  3. Start the server: node server.js
  4. Access the application at http://localhost:3000

Conclusion

One Hundred Million Checkboxes showcases advanced web development techniques, pushing the boundaries of what's possible in browser-based applications. By employing smart rendering strategies, efficient data structures, and optimized real-time communication, it achieves smooth performance even with an enormous number of interactive elements.

Future Enhancements

  • Implement server-side persistence for checkbox states
  • Add user authentication and personalized experiences
  • Optimize for even larger numbers of checkboxes (e.g., 1 billion)
  • Implement more advanced collaborative features

Created by Mohd Yahya Mahmodi

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published