I have to process loads of images. First, I need to check if the size of the image is greater than 50x60 and appropriately increasing the counter of bad images.
The problem I have is that the speed of n.width
/ n.height
on Internet Explorer 8 is extremely low. I checked n.offsetWidth
, n.clientWidth
but they are all the same speed-wise. I cannot use n.style.width
though, because this value is not always set on the <img />
tags that I'm interested in.
Consider following code:
Javascript
var Test = {
processImages: function () {
var fS = new Date().getTime();
var minimagew = 50,
minimageh = 60;
var imgs = document.getElementsByTagName('img');
var len = imgs.length,
isBad = 0,
i = len;
while (i--) {
var n = imgs[i];
var imgW = n.width;
var imgH = n.height;
if (imgW < minimagew || imgH < minimageh) {
isBad++;
}
}
var fE = new Date().getTime();
var fD = (fE - fS);
console.info('Processed ' + imgs.length + ' images in '
+ fD + 'ms. ' + isBad + ' were marked as bad.');
}
};
HTML
<img src="http://nsidc.org/images/logo_nasa_42x35.gif" />
[snip 9998 images]
<img src="http://nsidc.org/images/logo_nasa_42x35.gif" />
Code produces following output parsing 10k images (3 different Ctrl+F5s)
- FF: Processed 10000 images in 115ms. 10000 were marked as bad.
- FF: Processed 10000 images in 99ms. 10000 were marked as bad.
- FF: Processed 10000 images in 87ms. 10000 were marked as bad.
- IE8: Processed 10000 images in 206ms. 10000 were marked as bad.
- IE8: Processed 10000 images in 204ms. 10000 were marked as bad.
- IE8: Processed 10000 images in 208ms. 10000 were marked as bad.
As you can see the code in FF 3.6 is twice faster than the code executed in IE8.
To prove that the issue is really related to the speed of browser dimension property, if I change: n.width
and n.height
to constants, so we'll have:
var imgW = 43;
var imgH = 29;
I get following results:
- FF: Processed 10000 images in 38ms. 10000 were marked as bad.
- FF: Processed 10000 images in 34ms. 10000 were marked as bad.
- FF: Processed 10000 images in 33ms. 10000 were marked as bad.
- IE8: Processed 10000 images in 18ms. 10000 were marked as bad.
- IE8: Processed 10000 images in 22ms. 10000 were marked as bad.
- IE8: Processed 10000 images in 17ms. 10000 were marked as bad.
That's right! When we skip <img />
dimension check (call to node.width
/ node.clientWidth
etc), IE8 actually performs better than Firefox.
Do you have any ideas why does it take so long for IE to check the size of the image and eventually how to improve the performance of this check?