Actual table Vs. Div table

69,057

Solution 1

It is semantically incorrect to simulate data tables with divs and in general irrelevant to performance as rendering is instant. The bottle neck comes from JavaScript or extremely long pages with a lot of nested elements which usually in the old days is 100 nested tables for creating layouts.

Use tables for what they are meant to and div's for what they are meant to. The display table-row and cell properties are to utilize div layouts more then creating tables for data representations. Look at them as a layout columns and rows same as those you can find in a newspaper or a magazine.

Performance wise you have couple of more bytes with the div example, lol :)

Solution 2

In first instance, I wouldn't worry about performance, but more about semantics. If it's tabular data, use a <table>. If it are just block elements representing a layout element, use <div>.

If you really, really worry about performance, then the answer would still depend on the client used. MSIE for example is known to be slow in table rendering. You should at least test yourself in different browsers.

If this worriness is caused by large data, then I'd consider to introduce paging/filtering of the data you're about to show.

Solution 3

Just throwing in my two cents on the topic of performance. For small (under 100 rows, for example) tables, using DIVs wouldn't make much of a difference performance wise.

If you want to generate very long datasets, on the other hand, you hands down absolutely should use traditional HTML tables.

Brief Explanation:

This all spawned from my company's project, where I wrote a Javascript class to procedurally generate tables for me based on SQL data (it's a reporting kind of module). Anyway, I like DIVs so I wrote it to be DIV-based, much like the OP example.

After some horrendous performance (in IE8 particularly), I decided to re-write it using just tables since it's pretty simple tabular data, overall. Tables are, for whatever reason, about twice as fast on my machine in Chrome. The same is true for Safari.

That said, since I can't provide my work's code, I wrote a little benchmark thinggy that just let's you try either or methodology out. You'll see they're nearly identical, just one uses divs with styling to mimic the standard behavior of a table, and the other is just an old school table.

The only real difference is the type of element being generated, so that's about the only thing I can account for in the time delta. I'm sure it varies by browser, but it seems to me that table elements are just faster.

<script type="text/javascript">
var time_start = new Date().getTime();
var NUM_ROWS = 5000;
var NUM_CELLS = 8;
var OLD_STYLE = 1; // toggle 0/1, true/false
if(OLD_STYLE)
{
    var table = document.createElement('table');
    document.body.appendChild(table);
    for(var a = 0; a < NUM_ROWS; a++)
    {
         var row = document.createElement('tr');
         for(var b = 0; b < NUM_CELLS; b++)
         {
             var cell = document.createElement('td');
             cell.innerHTML = 'cell';
             row.appendChild(cell);
         }
         table.appendChild(row);
     }
}
else
{
    var table = document.createElement('div');
    document.body.appendChild(table);
    for(var a = 0; a < NUM_ROWS; a++)
    {
        var row = document.createElement('div');
        row.setAttribute('style','display: table-row; padding: 2px;')
        for(var b = 0; b < NUM_CELLS; b++)
        {
            var cell = document.createElement('div');
            cell.setAttribute('style','display: table-cell; padding: 2px');
            cell.innerHTML = 'cell';
            row.appendChild(cell);
        }
        table.appendChild(row);
    }
}
var dt = (new Date().getTime() - time_start)/1000;
console.log( dt + ' seconds' );
</script>

Solution 4

There are many discussions about this and div table usually gets the upper hand because of its flexibility in styling. Here's one link - http://css-discuss.incutio.com/wiki/Tables_Vs_Divs

Solution 5

I wanted to mention that if you use a table structure instead of div than users can hold CMD (or ALT in windows) to select a certain area of data from the table to copy. That data also pastes very easily into excel and other similar workbook programs.

Share:
69,057
Ben
Author by

Ben

Updated on July 08, 2022

Comments

  • Ben
    Ben almost 2 years

    This

    <table>
        <tr>
            <td>Hello</td>
            <td>World</td>
        </tr>
    </table>
    

    Can be done with this:

    <div>
        <div style="display: table-row;">
            <div style="display: table-cell;">Hello</div>
            <div style="display: table-cell;">World</div>
        </div>
    </div>
    

    Now, is there any difference between these two in terms of performance and/or render speed or they're just the same?

  • Thomas
    Thomas about 14 years
    That is not true if table-layout is set to fixed.
  • Ivo Sabev
    Ivo Sabev about 14 years
    Legacy code I guess, even the smallest change in layout of website like Google is hard. People say - If it works don't change it.
  • Dor
    Dor about 14 years
    That is browser-dependent. Firefox will render the table progressively but IE will not. blogs.msdn.com/ie/archive/2005/02/10/370721.aspx
  • Ivaylo Slavov
    Ivaylo Slavov about 12 years
    And how about the <thead>, <tfoot> and <tbody> tags? I read somewhere that rendering of tables is being improved because modern browsers recognized and rendered first <thead> and <tfoot> contents
  • John Dvorak
    John Dvorak over 10 years
    the reason could be the setAttribute calls. Can you make a test that injects HTML? Also note you're measuring the creation speed, not the rendering speed.
  • Dan H
    Dan H over 10 years
    Do you have any reason to believe that "injecting" the HTML would be somehow faster than just manipulating the DOM directly? Also, you'll note, I append the nodes as I make them, so you could probably say it measures both creation and rendering?
  • John Dvorak
    John Dvorak over 10 years
    " I append the nodes as I make them" - this slows down things quite substantially. Instead, you should build the table first, then append it to the document. Otherwise, each change that affects the document causes the browser to recalculate the layout again. What is the time difference if you build the table / faux-table first, then append it to the document once it's done?
  • Dan H
    Dan H over 10 years
    No measurable difference that I can tell. The biggest difference in speed is indeed setAttribute. In futility, I tried changing it to direct assignment as opposed to setAttribute (so for example cell.style.display = 'table-cell';) and there's seemingly no difference. If I don't assign any styles to the DIVs it executes as fast as the tables -- which is expected since it's just creating tags like the table. On the other hand, if you don't apply styling to the divs it doesn't reproduce the table effect.
  • John Dvorak
    John Dvorak over 10 years
    Hmm... another thought: if you set a style attribute as a string, it has to parse the whole value as CSS. Try setting style.display (to a string) and style.padding (in pixels if a number) instead.
  • tomasz86
    tomasz86 almost 8 years
    @IvayloSlavov thead and tfoot are used mainly for printing where they are supposed to be displayed on the top and bottom of each page (if the table in concern is longer than one page).
  • PirateApp
    PirateApp over 6 years
    and what do you say about responsiveness? because none of the table elements are remotely responsive except adding a horizontal scrollbar
  • MGOwen
    MGOwen almost 5 years
    @PirateApp do you mean responding to different screen sizes? Tables of data can also stretch or have contents wrap with the right styles. If it's an actual table of data - the only thing you should use <table> for - that's what you want.
  • mherzog
    mherzog about 3 years
    I cannot access the link in this post, and would like to, as I am new to this topic and would like to use the div-oriented techniques being discussed. Is there another reference I can use?