When it comes to this type of calculation, drawing the squares directly on a single canvas is likely more efficient. JavaScript now compiles into highly optimized native code, so the slight overhead you may experience is probably far less than dealing with the DOM and using .style
.
It's important to test your code on the specific devices and browsers you are targeting to get accurate results, as performance can vary over time.
Here is an example of how you could implement this:
let ctx = canvas.getContext("2d");
setInterval(function(){
for (var y=0; y<256; y+=16) {
for (var x=0; x<256; x+=16) {
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
ctx.fillRect(x, y, 16, 16);
}
}
}, 10);
<canvas id="canvas" width=256 height=256></canvas>