Even though there are areas for improvement in performance, it is important to note that the transition from 0% to 100% loading may appear faster on mobile than on a desktop due to various factors.
Web page images load in parallel, so multiple images finishing simultaneously can contribute to the quick progression from 0% to 100% completion. Additionally, desktop sites often serve larger image files compared to those served on mobile devices, resulting in longer loading times and more incremental progress on desktops.
It is essential to conduct testing across different desktops and mobile devices as internet speeds and hardware configurations vary, influencing loading times and overall performance results.
Regarding your code, here are some suggested modifications:
Your current script loops through all images once, offering only a snapshot of the loading progress at that moment. To provide accurate progress updates, consider setting up a load
event handler for each image to track loaded counts dynamically. The .load
event fires upon image completion without needing to test for .complete
.
Furthermore, using querySelectorAll()
instead of getElementsByTagName()
can enhance performance by avoiding unnecessary re-scanning of the DOM for elements, especially if dealing with static images.
Lastly, optimize your code by utilizing .textContent
when HTML parsing is not required, which reduces processing overhead.
// Create an array for image elements
var imgList = Array.prototype.slice.call(document.querySelectorAll("img"));
var numb = parent.document.getElementById("numb");
var imgListLength = imgList.length;
var start = 0;
// Iterate over image elements
imgList.forEach(function(img){
// Add a load event listener for each image
img.addEventListener("load", function(){
// Update count and percentage
numb.textContent = Math.round((++start / imgListLength) * 100);
});
});