Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to dynamically update the style of millions of DOM elements

I've built Conway's Game of Life in the browser just using the DOM. Nothing speical, it's been done before. My goal is to optimize it as best as I can. My actual Game of Life code works fine, and is fast enough for my liking. The bottleneck occurs in updating the screen state. With hundreds of thousands or millions of DOM elements on the screen, you can imagine that this can be quite slow (although faster than I first thought). My question is:

Working with upwards of a million DOM elements on the screen at a time, what is the fastest way to iterate through a list of DOM elements and individually change their style?

I'm using a class to keep track of styles, would it be better to dynamically change their style instead of class? I'm keeping all these elements in a multidimensional array, would it be better to iterate through another way (the loop itself is not a bottleneck, there are many such loops in my code that run fast enough for me)? I don't really know anything about "reflows" or how the browser renders things when elements change. Could these ideas be leveraged in a way that increases performance?

Here is my current code:

var updateUI = function () {
    for (var i = 0; i < width; i++) {
        var row = grid[i];
        var rowUI = gridUI[i];
        for (var j = 0; j < height; j++) {
            rowUI[j].className = "b" + row[j];
        }
    }
}

with the class style being:

.b1 {
    background: #000;
}

http://jsfiddle.net/WE5jQ/

like image 655
MattDiamant Avatar asked Nov 15 '13 22:11

MattDiamant


People also ask

What is generally the fastest method to retrieve an element from the DOM?

Accessing Elements by ID The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object.

Does jQuery make DOM manipulation faster?

jQuery is a wrapper that normalizes DOM manipulation in a way that works consistently in every major browser. It's fully reasonable for it to perform 25x slower than direct DOM manipulation. The performance loss is a tradeoff to have concise code. JavaScript in general is a highly asynchronous language.

Which method is used to add components or elements to the DOM?

The simplest, most well-known method of appending an element to the DOM is certainly the appendChild() .


2 Answers

Don't update the style of each one. Use the most general selector you can, and manipulate the actual style rules in the stylesheet. Let the browser do the rest of the work.

like image 64
Matt Ball Avatar answered Sep 21 '22 23:09

Matt Ball


You can divide the grid into smaller regions and than update style only to elements in regions which are considered 'dirty' (contain at least one changed element compared to previous iteration). That should significantly reduce the amount of operations you need to do in each update UI call, especially when we're talking about millions of elements.

like image 33
Mariusz Jamro Avatar answered Sep 21 '22 23:09

Mariusz Jamro