Pass submited file to web worker by refference with as little overhead as possible

走远了吗. 提交于 2019-12-23 12:39:07

问题


I will have a web worker to parse huge text file (200000 lines, simple syntax though). I expect user to submit that file wia drag'n'drop or otherwise, obtaining a File object:

   var work = new Worker("parser.js")
   document.addEventListener("drop", function(e) {
       e.preventDefault();
       var dt    = e.dataTransfer;
       var files = dt.files;
       if(files.length>0) {
         var firstFile = files[0]
         var reader = new FileReader();
         //SEND FILE TO WORKER?
       }
   });

I heard of Transferable objects. Is there a way to transfer file to Worker? In a way that GUI thread will not be slowed by reading the file?


回答1:


Some browsers (can't find a compatibility table) support passing File objects through the web worker postMessage because they now use the structured clone algorithm to handle the message parameters. This would probably be the most efficient method for those browsers which support it.

Further research seems to indicate that structured cloning is supposed to be available on: Chrome 13+, Firefox 8+, IE10+, Opera 11.5+, Safari 5.1+



来源:https://stackoverflow.com/questions/33306015/pass-submited-file-to-web-worker-by-refference-with-as-little-overhead-as-possib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!