An increasing number of DOM elements causing slowdown in performance

We developed a web application using asp.net mvc, javascript and jQuery hosted locally. Our approach involved keeping all HTML elements in the DOM object instead of destroying them, opting to hide and show pages as users navigate through the application.

As our application continues to expand, we have started encountering performance issues when navigating between pages. Upon inspecting the number of DOM elements in our page, we found that it has exceeded 20,000.

Some questions have arisen:


Is hiding/showing HTML pages a better design choice compared to destroying and re-creating them?

With 20,000 DOM objects manipulated by javascript, what impact does this have on performance in terms of reflow and repaint?

Answer №1

It's worth noting that the HTML5 single-page specification currently contains 109,000 DOM elements (and even more DOM nodes) as of now.

Is it better for design to hide/show HTML pages rather than destroying and recreating them?

The decision largely depends on whether it is necessary (for example, if recreating removed sections would be slow) and/or likely (if user interaction frequently causes hidden sections to be displayed again).

There are a few factors that can impact performance:

a) Garbage collection - More nodes mean more objects for the garbage collector to manage. While modern garbage collectors should handle long-lived objects efficiently, not all browsers have advanced GC mechanisms.

b) Reflow events, DOM queries, traversal operations, etc.

c) JavaScript code scalability with document size - Some algorithms may become inefficient in complex applications. Callbacks triggering additional callbacks can also contribute to slowdown.

While there may not be much you can do about point a), points b) and c) can be optimized in various ways:

  • Detach subtrees from the document and store them in JavaScript/document fragments to avoid impacting tree traversals.
  • Avoid interleaving actions that read layout state or write to the DOM. Perform reads and writes separately in bulk to minimize reflows.
  • Optimize performance hotspots in your JavaScript code.

However, these are general recommendations. It's essential to profile your application to identify CPU-intensive tasks and determine if the bottleneck lies in garbage collection, JavaScript execution, or paint events.

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

What is the best way to utilize date-fns for formatting a date retrieved from an API?

After receiving data from an API in the 'yyyy-MM-dd' format, I encountered a display issue when trying to convert it to 'dd-MM-yyyy'. How can I properly adjust the date format without disrupting the table display? onMounted(async () =& ...

Why is my Angular 2 service not showing up in my application?

Trying to access a JSON file using an Angular service has been unsuccessful. While I can easily read and bind the JSON data without the service, attempting to do so with the service results in an error message: Failed to load resource: the server responde ...

How can I incorporate a material-ui component using innerHTML?

Hi there, I'm new to using React and StackOverflow. I've been attempting to incorporate a Button component from material-ui by utilizing document.getElementById.innerHTML. However, upon the appearance of the button, the material-ui styling fails ...

Analyzing Data for Distributed / External Pages

Our team is currently exploring options for tracking metrics on content we produce for external pages. We are considering various approaches such as creating our own JavaScript, using a tracking pixel, or adapting Google Analytics to fit our needs. We wou ...

Calculating the angle formed by three vectors in three-dimensional space

According to the solution(s) provided in this post Angle between 3 points in 3d space, there is a JavaScipt function that calculates the angles between three vectors in 3D space. The function calculates the angles as if each vector is a vertex on a surface ...

Can someone assist me in determining the UV mapping process from Ricoh Theta S Dual Fish Eye to a Three.js r71 SphereGeometry?

Currently, I am attempting to replicate the three.js panorama dualfisheye example using Three.js version r71. It is crucial for me to adhere to r71 because I plan to integrate this code into the Autodesk Forge viewer, which relies on Three.js r71. I have ...

javascript: separate string into parts and substitute values

I woke up with a terrible hangover after celebrating my birthday yesterday. Can someone please provide a simple and straightforward JavaScript function to help me split a string and replace the necessary values? Keep it simple, stupid (KISS)! Here's ...

Embed information from a MySQL database into an HTML layout

I have a main webpage containing various links (e.g. fist_link, second_link, etc...) When a user clicks on any link, it should open blank_template.html. If the user clicks on fist_link, the template should display the first string from the database table. ...

Tips for converting a large number into a string format in JavaScript

I created this easy loan calculator by following online tutorials and using my basic coding skills. It works well, but I would like to add spaces in the output numbers for readability. For example, instead of "400000", I want it to display as "400 000". ...

The port is not defined in the express when running with the command "node ."

After going through the tutorial mentioned here, everything was smooth sailing until I reached the part where I had to run the server: https://www.digitalocean.com/community/tutorials/setting-up-a-node-project-with-typescript Attempting to execute the cod ...

Store the output of JavaScript in a PHP variable

My objective is to convert any image to base 64 and then store the converted string in a PHP variable before inserting it into my database. JavaScript Code: function uploadFile() { if (this.files && this.files[0]) { var FR= new FileReader ...

Are you experiencing a clash among various JS files in your WordPress installation?

I'm in a bit of a bind here. I've been tasked with working on a Wordpress website for a friend, using a free theme that he provided. Naturally, he wants some modifications made, so I've been editing the theme accordingly. The theme relies on ...

Display Issue with Header Row in React Data Table Component - Full Text Not Visible

Hey there! I've been working with a neat npm package called react-data-table-component. However, I've run into a bit of trouble trying to adjust the width of the table header text. Take a look at the issue here: https://i.sstatic.net/AzqNw.png ...

collection of assurances and the Promise.all() method

Currently, I am dealing with an array of Promises that looks like this: let promisesArray = [ service1.load('blabla'), service2.load(), // throws an error ]; My goal is to execute all these Promises and handle any errors that occur, as ...

Tips on implementing v-show within a loop

Hey @zero298, there are some key differences in the scenario I'm dealing with. My goal is to display all the items in the object array and dynamically add UI elements based on user input. Additionally, v-if and v-show function differently (as mentione ...

When you click on the "email" link, the Email app will automatically launch

Is there a way I can make the "EMAIL" text clickable in HTML, so that it automatically opens an email app or site and inserts the clicked email into the "To:" field? ...

Uncover the hidden message in a string encoded for JavaScript

This encoded HTML code is intended for use in a JavaScript function <div class=\"ProductDetail\"><div style=\"width:780px\">\r\n\t<div class=\"baslik\" style=\"margin: 0px; padding: 5px 10px ...

What are the steps to convert a canvas element, using an image provided by ImageService as a background, into a downloadable image?

I've been working on an app that allows users to upload an image, draw on it, and save the result. To achieve this functionality, I'm using a canvas element with the uploaded image as its background. The image is retrieved through ImageService. B ...

The onChange function is not triggered when the dropdown menu consists of only one item

I am facing an issue with my dynamically populated dropdown menu. When the dropdown contains only one element, the onChange() function of my select tag does not work as expected. It functions perfectly fine when there are multiple elements present. How c ...

Utilizing JavaScript's Array.sort() method for sorting objects with varying properties

Currently, I am dealing with these specific Objects: let obj1 = { from: Date, to: Date } let obj2 = { date: Date } These Objects have been placed in an Array: let arr = [ obj1, obj2 ] My goal is to organize the objects within the array by using arr.sort( ...