Executing multiple DOM updates with JavaScript efficiently

前端 未结 4 1300
梦毁少年i
梦毁少年i 2020-12-28 16:03

I have a HTML/JS website with several hundreds of div elements. A few dozens of these elements should be updated in a rapid fashion (up to 250 times per second) at one time

4条回答
  •  感动是毒
    2020-12-28 16:27

    It seems like you already have the basics of what you want to do. For your particular use case, I see no reason why you shouldn't just naively implement those methods.

    var imaginaryLibrary = function() {
        var toProcess = [];
    
        function collectDomUpdate(el, prop, newValue) {
            toProcess.push([el, prop, newValue])
        }
    
        function applyUpdates() {
            while (toProcess.length) {
                var actions = toProcess.pop()
                actions[0][actions[1]] = actions[2]
            }
        }
    }
    

    The above code is a skeleton of what you might want to do—it contains no error checking and assigns directly to object instead of potentially using DOM attributes.

提交回复
热议问题