最近,在 CoderWall 上 https://coderwall.com/p/rkls8g :
Make use of Web Workers
When it comes to do synchronous stuff like using the LocalStorage or XMLHttpRequest, It's way better to make use of workers to do jobs in a non-blocking manner ...
真的有必要吗?对于 AJAX,既然它已经是异步的,那么似乎没有必要引入更多使用 WebWorkers 的开销?
对于LocalStorage,参数是
LocalStorage's API writes all data directly into filesystem, so it has to lock the file. and it's done synchronously
这真的很慢吗?由于我希望只有 1 个应用程序(您的浏览器)为您的域写入 LocalStorage,所以它不太可能阻止它吗? LocalStorage 的上限只有 5MB?我看到写入 LocalStorage 的等待时间很长的唯一情况可能是如果您有多个选项卡/浏览器访问同一个 Web 应用程序并写入大量内容?即便如此,我想它应该被缓存,希望是回写缓存,然后它应该再次快速?
最佳答案
我相信大多数浏览器都会implement web storage如在内存存储中。因此,与使用 web worker 相比,您不会获得性能优势。
要处理多个选项卡/浏览器,您将使用 storage events .
如您所见,localStorage 并不慢,但有时读写不一致。除了使用像 IndexedDB 或 WebSQL 这样的事务性存储,你无能为力。
关于html - 仅将 WebWorker 用于 LocalStorage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13739657/