admin管理员组文章数量:1328037
I am currently researching better ways to acplish pre-loading of images and thought perhaps using web workers would be a more ideal way. However, I am not sure that it is. Is there any specific benefit in terms of performance and user experience using background workers versus classic-style pre-loading? Which is better?
function preloadBackground(a) {
new d(window.URL.createObjectURL(
new Blob([
"(function () {" +
"var x = new XMLHttpRequest();" +
"x.responseType = 'blob';" +
"x.open('GET', '" + a + "', true);" +
"x.send();" +
"}());\n"], { type: "text/javascript" })
)
)
}
function preloadClassic(a) {
var i = new Image();
i.src = a;
}
I am currently researching better ways to acplish pre-loading of images and thought perhaps using web workers would be a more ideal way. However, I am not sure that it is. Is there any specific benefit in terms of performance and user experience using background workers versus classic-style pre-loading? Which is better?
function preloadBackground(a) {
new d(window.URL.createObjectURL(
new Blob([
"(function () {" +
"var x = new XMLHttpRequest();" +
"x.responseType = 'blob';" +
"x.open('GET', '" + a + "', true);" +
"x.send();" +
"}());\n"], { type: "text/javascript" })
)
)
}
function preloadClassic(a) {
var i = new Image();
i.src = a;
}
Share
Improve this question
edited Dec 11, 2015 at 21:12
Joshua Dannemann
asked Dec 11, 2015 at 21:07
Joshua DannemannJoshua Dannemann
2,0802 gold badges16 silver badges35 bronze badges
7
- it gets it off the main thread. – Daniel A. White Commented Dec 11, 2015 at 21:08
- Yes, but with the classic means of pre-loading, doesn't the browser also load the images in the background? I was thinking that so long as there is no callback function with the onload event, that the two methods are roughly equivalent in terms of performance. Please correct me if I am wrong. – Joshua Dannemann Commented Dec 11, 2015 at 21:10
- 1 Only one surefire way to know, test it out. My hunch would be that the worker will be significantly slower. I'm pretty sure browsers are optimised to hell for images. – Matt Styles Commented Dec 11, 2015 at 21:12
- I could see it being faster or slower or not significantly different at all. You should set up a test and see which way is faster. – NoobsArePeople2 Commented Dec 11, 2015 at 21:12
- 1 I never realized that. Here's the link to the things available to workers html.spec.whatwg/multipage/… – 11thdimension Commented Dec 11, 2015 at 21:48
1 Answer
Reset to default 8You can pre-load images easily with web-workers and hide them until you are ready to display them. This increases page load speed and reduces jank so that the flow is seamless between click and load.
The main benefits to Pre-loading an image are usually seen when transitioning between a menu to a page with desired content, say a map or the next image in a slideshow or something similar. If you have a click event that takes a user to a picture then it's beneficial to pre-load. If you are testing with google's page speed insights you will notice a huge performance increase as well with a pre-load vs conventional load when clicked functionality.
Check out this code over at Github, I think you may find it useful.
If the image is loaded using a web-worker you avoid blocking the DOM construction so assigning a web worker to load the image when it's ready rather than when the rest of the page is will be helpful too, but that doesn't sound like pre-loading to me but rather an order of operations assignment. If the image isn't the content but rather the background it is unnecessary to make it DOM build blocking. Using a web-worker in this case is better.
本文标签: javascriptIs there an advantage to using web workers to preload imagesStack Overflow
版权声明:本文标题:javascript - Is there an advantage to using web workers to pre-load images? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742243647a2438993.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论