When a page becomes unresponsive due to an overload of DOM elements in a list, causing tabs to freeze

My webpage features a two-column layout with a header containing a navigation bar and a footer.

The left column is dedicated to listing items in three different tabs, each tab representing a different item type.

On the right side, one or more maps are displayed to showcase these items.

The issue arises when selecting a tab with a large number of elements, causing the interaction to freeze and impacting responsiveness. This can occur even when not directly interacting with the selected tab, such as hovering over a navigation link.

The page functions fine when the selected tab has fewer items. To illustrate my point, I have created a spike solution.

It's essential to note that this example is a simplified version of my problem, serving as a sample to present my case.

I have been using Chrome developer tools to track the timeline actions. It appears that the issue stems from reflows in the layout rendering caused by jQuery.

Refer to this screenshot of my timeline: https://i.sstatic.net/ex4wI.png

How can I optimize my DOM manipulation to prevent page freezing? Is there a way to instruct the browser not to redraw the left column until instructed?

Answer №1

Have you considered implementing virtual scroll, infinite scroll, or pagination for a large table instead of creating it all at once?

There are numerous examples available: https://www.sitepoint.com/jquery-infinite-scrolling-demos/

Placing 5k objects in a single line may be overwhelming, in my opinion.

In this scenario, it may be necessary to break up the dom manipulation loop for other tasks in the stack/event loop.

$('#addThousand').on('click', function () {
  for(var i = 5000; i > 0; i--){
  setTimeout( function() {
    var dom=createContactDom();
    $("#home div.panel-default").append(dom);
    }, 0);
  }

});

Alternatively:

$('#addThousand').on('click', function () {
  for(var i = 5000; i > 0; i--){
  setTimeout( function() {
    var dom=createContactDom();
    $("#home div.panel-default").append(dom);
    }, i);
  }

});

Answer №2

Tips for efficiently manipulating large amounts of elements within a DOM container using JavaScript

After conducting thorough testing and research, I have come to a solution that works effectively.

To optimize performance when hiding or showing elements in the DOM, it is recommended to avoid using 'display: none' and 'display: block' as they can impact the performance, especially with a large amount of data.

Instead, consider shifting the elements out of view without removing them from the DOM. By adjusting the position, the browser does not need to calculate if the content is present or not, resulting in improved performance. Keep in mind that implementing custom tabs may be necessary to override default behavior.

div {
  position: relative;
  left: 0px;
}
div.hide {
  left: -4096px;
  height: 0px;
} 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Selecting the correct data type for react-icons

I'm currently working on designing a navigation bar with a customized type to utilize the map() function. My goal is to display an icon for each entity, so that the icon appears in front of the text within the navbar. However, I am encountering diffic ...

Aligning a child element in the center along the horizontal axis within a parent container that is smaller in width than

I have been attempting to center an <img> element horizontally within a <div> that is narrower than the image itself. The <div> has overflow: hidden applied to it. For example, if the image is 500px wide and the DIV is 250px wide, is the ...

Preventing the image from being displayed when it cannot fully show is achieved through blocking the background repeat

Is there a way in CSS to only display a background image if it can be fully shown without setting the width in pixels? I want to avoid showing partial background images. Is there a CSS property that can help with this? [] <div align="left" style="pad ...

"Troubleshooting: How to Fix Issues with document.getElementById on Dynamic Div

Struggling to incorporate div elements and generate graphs with Google charts? The issue arises in the draw function, where attempts to access a div element using document.getElementById() result in null values and an error message stating "container not ...

Tips for inserting a personalized image or icon into the ANTD Design menu

Can anyone help me figure out how to replace the default icons with a custom image or icon in this example: https://ant.design/components/layout/#components-layout-demo-side I attempted to do it by including the following code: <Menu.Item to="/" key=" ...

Is the item positioned above another item instead of being positioned to the left of it?

Looking for help with my mobile navigation setup. I want to add text that says ' ...

Can the <article> tag and all its content be positioned in the center of the webpage?

Can the <article> and its content remain centrally aligned on the page? Check out my website here: Typically, when you visit a website, the article is positioned in the center of the page. However, in my case, the article only remains centered when ...

Stealthy Google Recaptcha integrated with a dynamic AJAX form

My website includes an ajax form: <form id="my_form"> <input type="text" id="field1" /> <input type="submit" value="submit" /> </form> I also have this JavaScript code: document.getElementById("my_form").onsubmit = fu ...

The script is malfunctioning on Google Chrome

Below is a script that I have: EXAMPLE : <script> var ul = document.getElementById("foo2"); var items = ul.getElementsByTagName("li"); for (var i = 0; i < items.length; i=i+2) { // perform an action on items[i], which repr ...

Toggle the visibility of images with input radio buttons

Explanation I am attempting to display an image and hide the others based on a radio input selection. It works without using label, but when I add label, it does not work properly. The issue may be with eq($(this).index()) as it ends up selecting a differ ...

What is the best way to replace a CSS Background that is already marked as important?

Attempting to change the background of a website using Stylish, but encountering issues. The existing background css on the website includes an !important rule which is preventing Stylish from taking effect. Here is my code: body { background-image: n ...

Is it possible to access the ID element of HTML using a variable in jQuery?

I have fetched some data from a JSON ARRAY. These values include Value1,Value2, and Value3. Additionally, I have an HTML checkbox with an ID matching the values in the array. My goal is to automatically select the checkbox that corresponds to the value re ...

Is there a way to delete a wrapper (parent element) in Angular without deleting the child element as well?

The solution provided in this answer on Stack Overflow addresses jQuery and not Angular or TypeScript. My inquiry bears resemblance to this question raised on a forum, but I am specifically looking for a resolution within the context of Angular. Is there ...

What is the best way to trigger a new css animation on an element that is already in the midst of

Let's talk about an element that already has an animation set to trigger at a specific time: .element { width: 100%; height: 87.5%; background: #DDD; position: absolute; top: 12.5%; left: 0%; -webkit-animation: load 0.5s ease-out 5s bac ...

Tips for dynamically populating an HTML table with data from a JSON array using JQuery

I have generated an HTML table <table id="top_five_table"> <tr> <td> </th> <th>URL</th> <th width="90">Total Hits</th> <th width="380">Percentage of all Hits</th> </tr> <tr> ...

Retrieve the erased document using Google's cache feature

After accidentally overwriting my .php web document file with an old version, I attempted to access the cached copy on Google. The only thing I could find was the HTML scripts, as the PHP coding was not visible. Now I am seeking a method to recover the e ...

Utilizing the HTML button element within an ASP file combined with JavaScript can lead to the reloading of the

I am attempting to create a JavaScript function for an HTML button element. However, I have noticed that it causes the page to reload. Strangely, when I use an input with a button type, everything works perfectly. What could be causing this issue and why a ...

When the radio button is selected, show a variety of buttons

I'm facing an issue with rendering different buttons for two radio buttons within a view. Here is the rendered HTML code for the radio buttons: <input checked="checked" id="Isattending_0" name="Isattending" type="radio" value="Yes" /> <inpu ...

What is the method for implementing right/left text ellipses on flexed buttons that span the entire viewport width using CSS?

I currently have the following code: .viewport { width: 300px; } .container { --color-dark: rgb(40, 40, 40); --color-light: rgb(255, 255, 255); --color-base-background: rgb(255, 255, 255); ... <div class="viewport"> <di ...

Font size too large when using the em unit

The default font-size of my code is set to 10px or 0.625em. According to this, to make the font size of <p> 7px, I should use 0.7em. However, in my case, the browser is consistently using a fixed font size of 9px, even when I decrease the font size o ...