Images in browsers performance for background-repeat CSS

When not repeating the background image, the time required to render depends on only the final scaled image, not the original one.

The image in a file is compressed as PNG format, but after being loaded by browser, it is in RGBA bitmap format (4 bytes for a pixel). When repeating a background, (let say on Intel x86), the native code of browser would use REP MOVSD to move the bitmap data from RAM to video memory (this is standard sequence, might be different on various implementations of graphics driver or specific GPU).

Assume that the dimensions of the HTML DIV which contains the background would be 100×100.

For the only-1 pixel image: the browser programme has to exec 10 thousand ‘REP MOVSD’ instructions.

For the 10×10 image: with each repeated image, the browser programme has to exec ‘REP MOVSD’ only 10 times (1 time calling to ‘REP MOVSD’ can render 1 pixel line (pixel row) of the image). So in this case, the number of ‘REP MOVSD’ instructions executed would be only 10×100 times (10 times in 1 image, 100 repeated images). This takes totally 1 thousand ‘REP MOVSD’.

Therefore, the final background based on the bigger image would be rendered faster.

More notes:
The above explanation doesn’t mean the performance is exactly 10 times better for the 10×10 image. A ‘REP MOVSD’ (with CX=9999 for example) is only 1 single CPU instruction but still requires 9999×4 bytes to be transfered through data bus. If using 9999 simple ‘MOV’s, that much of data still have to go thru’ data bus, however, CPU has to execute 9998 instructions more. A more clever browser would create a pre-rendered bitmap for the background with replicated images; so each time it needs to transfer to video memory, it needs just only 100 ‘REP MOVSD’ (100 is the number of pixel rows in the final background, assumed above) instead of 10 thousand or 1 thousand.

Leave a Comment