Tips to Avoid Layout Thrashing

Layout thrashing happens when JavaScript accesses certain properties that require the browser to recompute the layout of the web page. This process is costly because the browser needs to recalculate the size and position of elements based on their styles and content.

The following JavaScript operations can trigger layout thrashing:

  1. Reading Layout Properties: Accessing properties like offsetWidth, offsetHeight, clientWidth, clientHeight, getComputedStyle, or scrollWidth can trigger layout recalculations.
  2. Modifying Styles and Dimensions: Changing styles (e.g., or adding/removing DOM elements may cause layout changes.

To minimize layout thrashing and improve your web application’s performance, follow these best practices:

  1. Batch DOM Read and Write Operations: Minimize the number of times you read layout-affecting properties (offsetWidth, offsetHeight, etc.) and write to the DOM. Instead, batch these operations together.
    // Bad practice (triggers layout thrashing) 
    const width = element.offsetWidth;
    const height = element.offsetHeight; 
    // Better approach (batch reads and writes)
    const styles = getComputedStyle(element);
    const width = element.offsetWidth; // No thrashing here
    const height = element.offsetHeight; // No thrashing here = (width + 10) + 'px'; // Apply styles once
  2. Use Classes for Style Changes: Instead of modifying individual style properties directly, leverage CSS classes and toggle them on/off for style changes. This reduces the number of layout recalculations.
    // Bad practice (triggers layout thrashing) = (element.offsetWidth + 10) + 'px';
    // Better approach (uses CSS classes)
  3. Cache Layout Properties: Store layout-affecting properties in variables to avoid repetitive calculations.
    // Bad practice (repeated layout calculations)
    for (let i = 0; i < elements.length; i++) {
      const width = elements[i].offsetWidth; // Layout thrashing 
      // Use width...
    // Better approach (caches layout properties)
    const widths = [];
    for (let i = 0; i < elements.length; i++) {
      widths[i] = elements[i].offsetWidth; // No thrashing 
    // Use widths[i]...
  4. Optimize CSS: Optimize your CSS to minimize layout changes caused by style modifications. Avoid forced synchronous layouts by ensuring efficient CSS rules.

What is Load Balancing?

Load balancing is all about making sure no single server gets overwhelmed. Picture this: your favorite website or app can get crazy busy at times. Without load balancing, one server might end up feeling like it’s carrying the weight of the world, leading to slow response times or even crashing.

So, what does load balancing do? It’s like a traffic cop for network requests, spreading them out among multiple servers. This way, no server gets too bogged down, and everything runs smoothly.

Load Balancing Algorithms

  1. Round Robin: Servers take turns handling requests.
  2. Least Connections: The server with the least load (fewest active connections) gets first dibs on new requests.
  3. Weighted Round Robin: This one’s like Round Robin, but servers can have different “weights” based on their capacity – heavier servers get more requests.
  4. Least Response Time: Sends traffic to the server that’s quickest to respond – it’s all about efficiency.
  5. Hash: Distributes requests based on a key you define, whether it’s the client’s IP address or the request URL. If the set of servers changes, NGINX Plus can even apply a consistent hash to minimize the reshuffling of workloads.
  6. IP Hash: The IP address of the client decides which server gets the special treatment. So, when a request comes in, the server is chosen based on the client’s IP address.
  7. Random with Two Choices: This one randomly picks two servers and then makes a smart move. It applies the Least Connections algorithm to decide which server gets the request.

Benefits of Load Balancing

  • Scalable
  • Reduces downtime
  • Flexible
  • Efficient

Whether it’s at the application layer, transport layer, or network layer, load balancing ensures everything is running smoothly.