Simple html vs Javascript generated html?

前端 未结 12 1575
故里飘歌
故里飘歌 2020-12-30 13:09

In my web application I would like to complately avoid html and use only javascript to create web-page\'s dom tree.

What is faster writing web content in the traditi

12条回答
  •  独厮守ぢ
    2020-12-30 13:38

    2019

    (Probably the correct answer is correct for 2010). Here's an answer for 2019 with benchmark.

    Generating table with nested div, total of 500 rows, 250k table cells, 10 nested divs per table cell.

    
    
        
        
            ';
                for($j = 0; $j < 500; ++$j) {
                    echo '';
                }
                echo '';
            }
            ?>
            
    ' . $i . '/' . $j . '

    And the below HTML generated by JS:

    
        
            
        
        
            
        
    
    

    Approx. Result:

                          | Download Size  | Time 'til browser stops loading
    --------------------------------------------------------------------------
    Pure HTML by server   | 680,000 bytes  | 00:01:48 
    HTML generated by JS  |     570 bytes  | 00:00:28 
    

    Tested it on Chrome v70 on Ubuntu 18.

    Conclusion

    Always stick to normal HTML for many reasons (usually for readable/maintainable code), except if you're dealing with huge HTML then you may consider generating it by JS.

提交回复
热议问题