I need to modify each cell of a 100 columns x 50 rows HTML <table>
each time a key is pressed. This works, but there is a 250ms - 500ms delay before the rendering is done on my machine.
(The code snippet below has to be opened in "full page" to see this effect, you can use the arrow keys to trigger keydown events).
How to speed up the rendering of such a 5000-cell HTML table?
Note:
I need to keep 100 columns and fixed width (each cell takes 1% of the window width), and fixed height for each row, thus the
overflow: hidden
CSS ruleshere I used one random alphanumeric for each cell, but in my code, it is another content (still one char per cell)
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
window.onkeydown = function(e) {
// very fast from here ...
var start = new Date();
var s = '';
for (i = 0; i < 50; i++) {
s += '<tr>';
for (j = 0; j < 100; j++) {
s += '<td>' + characters.charAt(Math.floor(Math.random() * 62)) + '</td>';
}
s += '</tr>\n';
}
console.log(new Date() - start);
// ... to here: ~ 3 ms on average, no need to optimize the previous lines?
document.getElementById('main').innerHTML = s;
document.body.offsetHeight; // optional, triggers reflow/repaint to measure elapsed time more accurately
console.log(new Date() - start);
};
window.onkeydown();
* { padding: 0; margin: 0; border: 0; font-family: sans-serif; }
body, html { width: 100%; }
td { width: 1%; overflow: hidden; }
tr { line-height: 15px; overflow: hidden; white-space: nowrap; }
table { table-layout: fixed; width: 100%; border-collapse: separate; border-spacing: 0; }
<table id='main'>
</table>