With all the emphasis on site speed and a rise in the adoption of SVGs for web graphics due to increased use of retina type screens, optimising vector graphics has never been more important but not everyone goes to the trouble of ensuring this is always the case. One of the most popular SVG optimisers is SVGO and the tool can be used online here, but don’t assume once the tool has been run that your file cannot be optimised any further.

My top tip to any designer/developer producing SVGs in packages like Illustrator or Inkscape, is to become familiar with the raw code and understand exactly what is going on “underneath the hood” with these files. Once you have got to grips with this, it becomes a lot easier to study the code and see where further optimisations can be made.

As an example I am going to start with a logo that has already been passed through SVGO and is currently 2.04kb in its raw size.

Here is the code (spaced out onto new lines to make easier reading):

The first thing to notice is the viewbox, the first two values represent the x,y position of the graphic, and the last two the width and height. Now it may be only a small optimisation, but by adjusting the image starting position to 0,0 in Illustrator and re-running it through SVGO we save 7 bytes.

The next thing is to look at the image and see if any elements are duplicated, which in our example is the volume graphic either side of the wording. By defining this as one single plain path, we can use it twice in the file, applying a red fill firstly, and then lastly a white fill and a horizontal transform filter. To do this, we take the first path that starts M21.8 and place it in a definition tag. Next we need to find the white volume path and remove it all together. If you look at the last path you can see in the co-ordinates that there a value of M419.8 which is the start of the white volume path so we can delete this segment from that path. Now all we need to do is create two use tags for the graphic we are going to duplicate and then tidy up the class references which are no longer needed. As a further optimisation any instances of ‘zM’,’z M’ or ‘z m’,’zm’ in a path can be replaced by a single ‘M’ or ‘m’ respectively and similarly any path ending in a ‘z’ can be removed as well saving more precious bytes’. With all these optimisations, we now have a file below that is now only 1.47kb in size a 27% saving on the optimised original.

Whilst these optimisations may seem quite small when applied to multiple files, the bandwith savings can be significant.

Add a comment