Notes about web site optimizations, based on my tasks on optimizing Yandex search result page.
When you optimizing size keep in mind that final result will be gziped before network transfer. Every comparision must be done after compression.
Let's see an example.
This is a function to detect if browser has SVG support or not:
At first look you may consider that this is optimal solution and there is nothing to improve there. We used function, passed repeated parts as one letter params.
We can do better! The code below is bigger (161 bytes versus 157), but after gzip it is smaller (126 bytes versus 154):
And it is easier to read IMHO.
The same for the code which detects retina display:
After "de-optimizing" it is smaller and easier to read (191 byte versus 125):
Let's see an example.
This is a function to detect if browser has SVG support or not:
(function(d,n){d.documentElement.className="ua_noscript_"+(d[n]&&d[n]("http://www.w3.org/2000/noscript","noscript").createSVGRect?"yes":"no")})(document,"createElementNS")At first look you may consider that this is optimal solution and there is nothing to improve there. We used function, passed repeated parts as one letter params.
We can do better! The code below is bigger (161 bytes versus 157), but after gzip it is smaller (126 bytes versus 154):
document.documentElement.className=document.createElementNS&&document.createElementNS("http://www.w3.org/2000/noscript","noscript").createSVGRect?"ua_noscript_yes":"ua_noscript_no"And it is easier to read IMHO.
The same for the code which detects retina display:
(function(w,e,s,c,x,l,p,d){c="className";x="deviceXDPI";l="logicalXDPI";p="devicePixelRatio";d=x in s&&l in s&&s[x]/s[l]||p in w&&w[p];d>1&&(e[c]+=" i-ua_retina_yes")})(window,document.documentElement,screen)After "de-optimizing" it is smaller and easier to read (191 byte versus 125):
(screen.deviceXDPI&&screen.logicalXDPI&&screen.deviceXDPI/screen.logicalXDPI||window.devicePixelRatio)>1&&(document.documentElement+=" i-ua_retina_yes")About SVG
SVG is a text format and in most cases it will be compressed before network transfer, so do any comparision after gzip (see above).
I advice to choose SVG in most cases bacause of its vector nature. It scales well on any device.
You can insert SVG in DOM, as an image using IMG or place it on background in CSS. When an image is small and is used as decoration it is better to place it in CSS background property as data:uri (https://en.wikipedia.org/wiki/Data_URI_scheme) using URLEncode method (https://en.wikipedia.org/wiki/Percent-encoding).
I use SVGO (https://github.com/noscript/noscripto) to optimize my images with such parameters:
Typically it is enough.
But if you want to fine-tune SVG image and achieve minimal image size you need "to go deeper". In some cases it is better to "draw" an image in text editor instead of visual one. So I suggest to learn SVG internals, e.g. using this tutorial: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial
And one more rarery used thing about SVG optimization. Usually SVG has the same dimentions as will be used on web page. In such case vector coordinates may be fractional and it can increase file size. You may scale image to place vector points on pixel grid.
Example:
https://yastatic.net/web4/_/BM4YHAtH2zfvUiPvyhZHMkaXw4w.noscript
BTW, you may find an example of xlink usage there but that's not always the best way. Sometimes gzip works better than symbol.
SVG is a text format and in most cases it will be compressed before network transfer, so do any comparision after gzip (see above).
I advice to choose SVG in most cases bacause of its vector nature. It scales well on any device.
You can insert SVG in DOM, as an image using IMG or place it on background in CSS. When an image is small and is used as decoration it is better to place it in CSS background property as data:uri (https://en.wikipedia.org/wiki/Data_URI_scheme) using URLEncode method (https://en.wikipedia.org/wiki/Percent-encoding).
I use SVGO (https://github.com/noscript/noscripto) to optimize my images with such parameters:
noscripto -p 2 --multipass --enable=removeDesc --enable=removeTitle --enable=sortAttrs --enable=removeViewBox --enable=removeStyleElementTypically it is enough.
But if you want to fine-tune SVG image and achieve minimal image size you need "to go deeper". In some cases it is better to "draw" an image in text editor instead of visual one. So I suggest to learn SVG internals, e.g. using this tutorial: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial
And one more rarery used thing about SVG optimization. Usually SVG has the same dimentions as will be used on web page. In such case vector coordinates may be fractional and it can increase file size. You may scale image to place vector points on pixel grid.
Example:
https://yastatic.net/web4/_/BM4YHAtH2zfvUiPvyhZHMkaXw4w.noscript
BTW, you may find an example of xlink usage there but that's not always the best way. Sometimes gzip works better than symbol.