/    Sign up×
Community /Pin to ProfileBookmark

Are subsequent image loads on same page faster?

Let’s say some image appears on an html page, and so is ‘loaded’ by the browser for display…

<div><img src=somefile.jpg” </img></div>

And then on the same page ,the same image src is ‘loaded’ again, maybe this time in a javascript chain of calls, triggered by the page body’s onload event.

.

[code=html]
<script>
function getImg() {
var img = new Image();
img.onload = function() {return img;}
img.src=’somefile.jpg’; // same file already on the page
}
</script>

<body onload=”getImg()” <body>
[/code]

Will the second ‘load’ take as much time and memory as the first load of the page, or will it more likely be pulled from browser cache, and take minimal resources.

The background is, i need the original pixel width and height of certain images for subsequent layout and style calculations. Up till now I’ve been doing this server side with PHP, but I think a javascript solution might be better. And modern browsers *DO* provide the “naturalWidth” and “naturalHeight” properties of already loaded images! But older browsers (like IE8) can’t do it, and can only provide the current browser displayed dimensions. In those cases I can fall back to doing it with a javascript call to a new img object, as in the example above, But I’d like to know if in those cases, I’m forcing the browser to slow down by actually ‘loading’ the same image again. I’m thinking and hoping it would be simply referenced in cache, but i don’t know if I could prove it.

to post a comment

12 Comments(s)

Copy linkTweet thisAlerts:
@rootFeb 02.2019 — Things get cached.

End of story.

Also, Image tags have no closing element, its &lt;img attributes="values" &gt;
Copy linkTweet thisAlerts:
@PeterPan_321authorFeb 02.2019 — So @root#1600675, Yes I know things get cached, but in my example (sans the error you pointed out, would the javascript result in pulling the image from cache? Even if the full URL is passed? Any way i could prove it?
Copy linkTweet thisAlerts:
@SempervivumFeb 02.2019 — Any way i could prove it?[/quote]Use the developer tools of your browser. In my browser (opera) things are logged in the tab "network" and "from cache" is displayed if the element is loaded from cache.
Copy linkTweet thisAlerts:
@PeterPan_321authorFeb 03.2019 — Hmmm... and sadly the "worst case" (the browser where I MUST actually "load" images a second time, which is IE8) doesn't seem to have that in its network tools. What I'll have to do it add some "before"/"after" timers, and check the difference between loading an image already on the page, and an identically sized image that is NOT.
Copy linkTweet thisAlerts:
@rootFeb 03.2019 — I D K what you want to prove.

If you present a unique URL to any server, the request is treated as one that is not cached otherwise it should be in the cache.

So what are you trying to prove?
Copy linkTweet thisAlerts:
@shadewFeb 06.2019 — @root#1600675 Quick question on this. Do img tags have a self-closing tag /&gt; or open tag as you showed &gt;
Copy linkTweet thisAlerts:
@PeterPan_321authorFeb 08.2019 — @shadew#1600745 It was my bad copy. Closed tags.
Copy linkTweet thisAlerts:
@PeterPan_321authorFeb 08.2019 — Whether an image already on the page, by virtue of <img src = "imagessomeImg.jpg"> will be cached, so that a subsequent javascript referring to the SAME (not unique) URL, such as...

[code=javascript ]
var img = new Image();
img.onload = function() { w = this.width; h= this.height; }
img.src = = "imagessomeImg.jpg"; // same image loaded in page
[/code]


will take much less time (if any) to complete the onload event, since the image is already in browser (cache) memory.

As as as i've been able to see, the second image load happens in something less than 1mS (about the best time resolution i can measure in JavaScript) , so I'm confident at this point that subsequent loads of the SAME image are being pulled from cache.
Copy linkTweet thisAlerts:
@rootFeb 08.2019 — Again, IDK what you are trying to prove or do.

It seems a pointless task given the way a browser works and the image loading you have via javascript is nothing special other than you can load images in background and then display them, it was the earliest mode of loading control you could have with websites that were designed with tools like Macromedia Fireworks.

This is a way of preloading the images that you may want to display when a next button is clicked.

You are able to preload the image at the time the main image is displayed. I wrote a gallery that would preload images only when the next button was clicked, then the next image would show and the next image to be shown would be queued.

It saved on bandwidth that way allowing the progress to be incremental, clicking back would take back to the previous image, because it was preloaded, it was already available.

This was achieved with storing new Image() references in to an array.

imagesNeedingLoading = ["sunset","shoreline","mountains","lakes","nightsky"];
imagesLoaded = [];
while(imagesNeedingLoading.length){
loadImage = imagesNeedingLoading.shift();
img = new Image();
img.name = loadImage;
img.src = "./assets/headers/"+loadImage+".jpg";
imagesLoaded.push( img );
}
targetImage = document.images.placeholdername;
targetImage.src = imagesLoaded[0]; // first image



&lt;img name="placeholdername" src="first.jpg" height="100" width="300"&gt;

Is an example of a type of loader I used first, all from memory, no idea if it works because I am sure there is something missing but you should get the general idea of what I was doing to load.

for more info, look this up on MDN or W3Schools.
Copy linkTweet thisAlerts:
@PeterPan_321authorFeb 10.2019 — @root#1600866 I appreciate that, and know most of what you told me. I don't understand, however, why you tell me "So... what are you trying to prove", and then when I tell you what I was trying to prove, you respond with "Again, IDK what you are trying to prove or do".

I've written a few galleries too pre-loading images. This is a different case, and a different reason for the concern. But you've already said You don't care" what I'm trying to do, so I'll leave it there. Again, thanks for taking the time to answer. That in itself is very much appreciated.
Copy linkTweet thisAlerts:
@rootFeb 10.2019 — I have made it perfectly clear, if you want absolute confirmation, I suggest you head over to the W3C who write the standard white papers for web standards on how browsers are to work, how they achieve these tasks is down to the individual vendors, as long as document.getElementsById() works in MSIE, Mozilla Firefox, Chrome and all the other browsers, how it achieves this is in the developers hands, the main thing is the actual desired result is the expected output to the standard written.

Go here : https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html and you can read up about how a web browser IS MEANT TO HANDLE IMAGES AND CACHED ITEMS...

I have tried to explain to you that the cache caches items but you're wanting proof.... Sorry but this really is a lost cause and having asked you before to clarity, have to say that it hasn't been forthcoming so I can only assume that you need to be pointed in the right direction and hopefully you can argue with the W3C for proof.
Copy linkTweet thisAlerts:
@rootFeb 10.2019 — {"locked":true}
×

Success!

Help @PeterPan_321 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...