通过引用以尽可能少的开销将提交的文件传递给 Web Worker [英] Pass submited file to web worker by refference with as little overhead as possible

查看:27
本文介绍了通过引用以尽可能少的开销将提交的文件传递给 Web Worker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将有一个网络工作者来解析巨大的文本文件(200000 行,虽然语法简单).我希望用户通过拖放或其他方式提交该文件,获取 File 对象:

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?
       }
   });

我听说过可转移对象.有没有办法将文件传输给 Worker?以某种方式不会因读取文件而减慢 GUI 线程?

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?

推荐答案

某些浏览器(找不到兼容性表)支持通过 web worker postMessageFile 对象> 因为他们现在使用结构化克隆算法来处理消息参数.对于支持它的浏览器来说,这可能是最有效的方法.

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.

进一步研究似乎表明结构化克隆是应该适用于:Chrome 13+、Firefox 8+、IE10+、Opera 11.5+、Safari 5.1+

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+

这篇关于通过引用以尽可能少的开销将提交的文件传递给 Web Worker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆