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

MattDiamant

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/

Mariusz Jamro

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JS or jQuery way to dynamically create DOM elements

From Dev

What is the fastest way to empty a range of elements from the DOM

From Dev

Fastest way to import millions of JSON documents to MongoDB

From Dev

The fastest way to update (partial sum of elements with complex conditions) the pandas dataframe

From Dev

Fastest way to compare row and previous row in pandas dataframe with millions of rows

From Dev

Fastest way to get elements in viewport

From Dev

OpenMP fastest way to update a struct

From Dev

SQL - Fastest way to do an UPDATE

From Dev

Angularjs - dynamically add and update DOM

From Dev

Java bean: Fastest way to implement an iterator of elements?

From Dev

fastest way to animate a lot of graphic elements in JavaFX

From Dev

Python - Fastest way to find elements in XML documents

From Dev

Best/Fastest Way To Change/Access Elements of a Matrix

From Dev

fastest way to check if all elements in an array are equal

From Dev

Fastest way to find number of elements in a range

From Dev

fastest way to add elements in list in R

From Dev

What is the fastest way to access elements in a nested list?

From Dev

What is the fastest way to count elements in an array?

From Dev

Fastest way to populate a grid with elements randomly

From Dev

The fastest way to add elements in set collection

From Dev

Fastest way of iterating and accessing elements of numpy array?

From Dev

What is the fastest way to update a variable on a condition?

From Dev

What is the fastest way to conditionally update data in a DataTable?

From Dev

Fastest Way to Update table in SQL Server

From Dev

What is the fastest way to update many rows in a database?

From Dev

What is the fast way to update with 100 millions+ of records?

From Dev

Smooth way to repeat DOM elements

From Dev

How to apply style sheets to dynamically created elements?

From Dev

Changing style for children elements of dynamically created jQuery

Related Related

  1. 1

    JS or jQuery way to dynamically create DOM elements

  2. 2

    What is the fastest way to empty a range of elements from the DOM

  3. 3

    Fastest way to import millions of JSON documents to MongoDB

  4. 4

    The fastest way to update (partial sum of elements with complex conditions) the pandas dataframe

  5. 5

    Fastest way to compare row and previous row in pandas dataframe with millions of rows

  6. 6

    Fastest way to get elements in viewport

  7. 7

    OpenMP fastest way to update a struct

  8. 8

    SQL - Fastest way to do an UPDATE

  9. 9

    Angularjs - dynamically add and update DOM

  10. 10

    Java bean: Fastest way to implement an iterator of elements?

  11. 11

    fastest way to animate a lot of graphic elements in JavaFX

  12. 12

    Python - Fastest way to find elements in XML documents

  13. 13

    Best/Fastest Way To Change/Access Elements of a Matrix

  14. 14

    fastest way to check if all elements in an array are equal

  15. 15

    Fastest way to find number of elements in a range

  16. 16

    fastest way to add elements in list in R

  17. 17

    What is the fastest way to access elements in a nested list?

  18. 18

    What is the fastest way to count elements in an array?

  19. 19

    Fastest way to populate a grid with elements randomly

  20. 20

    The fastest way to add elements in set collection

  21. 21

    Fastest way of iterating and accessing elements of numpy array?

  22. 22

    What is the fastest way to update a variable on a condition?

  23. 23

    What is the fastest way to conditionally update data in a DataTable?

  24. 24

    Fastest Way to Update table in SQL Server

  25. 25

    What is the fastest way to update many rows in a database?

  26. 26

    What is the fast way to update with 100 millions+ of records?

  27. 27

    Smooth way to repeat DOM elements

  28. 28

    How to apply style sheets to dynamically created elements?

  29. 29

    Changing style for children elements of dynamically created jQuery

HotTag

Archive