javascript Web Workers 与 Promises
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20929508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web Workers vs Promises
提问by Mario
In order to make a web app responsive you use asynchronous non-blocking requests. I can envision two ways of accomplishing this. One is to use deferreds/promises. The other is Web Workers. With Web Workers we end up introducing another process and we incur the overhead of having to marshal data back and forth. I was looking for some kind of performance metrics to help understand when to chose simple non-blocking callbacks over Web Workers.
为了使 Web 应用程序响应,您使用异步非阻塞请求。我可以设想两种方法来实现这一点。一种是使用延迟/承诺。另一个是Web Workers。使用 Web Workers,我们最终引入了另一个过程,并且产生了必须来回编组数据的开销。我一直在寻找某种性能指标来帮助理解何时选择简单的非阻塞回调而不是 Web Workers。
Is there some way of formulating which to use without having to prototype both approaches? I see lots of tutorials online about Web Workers but I don't see many success/failure stories. All I know is I want a responsive app. I'm thinking to use a Web Worker as the interface to an in-memory data structure that may be anywhere from 0.5-15MB (essentially a DB) that the user can query and update.
是否有某种方法可以制定使用哪种方法而不必对两种方法进行原型设计?我在网上看到很多关于 Web Workers 的教程,但我没有看到很多成功/失败的故事。我只知道我想要一个响应式应用程序。我正在考虑使用 Web Worker 作为内存数据结构的接口,该数据结构可能介于 0.5-15MB(本质上是一个 DB)之间,用户可以查询和更新。
As I understand javascript processing, it is possible to take a single long-running task and slice it up so that it periodically yields control allowing other tasks a slice of processing time. Would that be a sign to use Web Workers?
据我了解 javascript 处理,可以采用单个长时间运行的任务并将其切片,以便它定期产生控制权,允许其他任务分配处理时间。这是使用 Web Workers 的标志吗?
回答by miraculixx
`Deferred/Promises and Web Workers address different needs:
`Deferred/Promises 和 Web Workers 满足不同的需求:
Deferred/promise are constructs to assign a reference to a result not yet available, and to organize code that runs once the result becomes available or a failure is returned.
Web Workers perform actual work asynchronously (using operating system threadsnot processes - so they are relatively light weight).
Deferred/promise 是为尚未可用的结果分配引用的构造,并组织在结果可用或返回失败后运行的代码。
Web Workers 异步执行实际工作(使用操作系统线程而不是进程 - 因此它们的重量相对较轻)。
In other words, JavaScript being single threaded, you cannot use deferred/promises to runcode asynchronously — once the code runs that fulfills the promise, no other code will run (you may change the order of execution, e.g. using setTimeout()
, but that does not make your web app more responsive per se). Still, you might somehow be able to create the illusion of an asynchronous query by e.g. iterating over an array of values by incrementing the index every few milliseconds (e.g. using setInterval), but that's hardly practical.
换句话说,JavaScript 是单线程的,你不能使用 deferred/promises异步运行代码——一旦代码运行完成了承诺,就不会运行其他代码(你可以改变执行顺序,例如 using setTimeout()
,但这不会使您的网络应用程序本身更具响应性)。不过,您可能会以某种方式创建异步查询的错觉,例如通过每隔几毫秒增加索引(例如使用 setInterval)来迭代值数组,但这几乎不切实际。
In order to perform work such as your query asynchronously and thus off load this work from your app's UI, you need something that actually works asynchronously. I see several options:
为了异步执行诸如查询之类的工作,从而从应用程序的 UI 中卸载这项工作,您需要一些真正异步工作的东西。我看到几个选项:
use an IndexedDBwhich provides an asynchronous API,
run your own in-memory data structure, and use web workers, as you indicated, to perform the actual query,
use a server-side script engine such as NodeJSto run your code, then use client-side ajax to start the query (plus a promise to process the results),
use a database accessible over HTTP (e.g. Redis, CouchDB), and from the client issue an asynchronous GET (i.e. ajax) to query the DB (plus a promise to process the results),
develop a hybrid web app using e.g. Parse.
使用提供异步 API的IndexedDB,
运行您自己的内存数据结构,并按照您的指示使用 Web Worker 来执行实际查询,
使用服务器端脚本引擎(例如NodeJS)运行您的代码,然后使用客户端 ajax 开始查询(加上处理结果的承诺),
使用可通过 HTTP 访问的数据库(例如 Redis、CouchDB),并从客户端发出异步 GET(即 ajax)来查询数据库(加上处理结果的承诺),
使用例如Parse开发混合网络应用程序。
Which approach is the best in your case? Hard to tell without exact requirements, but here are the dimensions I would look at:
哪种方法最适合您?没有确切的要求很难说,但这里是我要看的尺寸:
- Code complexity — if you already have code for your data structure, probably Web Workers are a good fit, otherwise IndexedDB looks more sensible.
- Performance — if you need consistent performance a server-side implementation or DB seems more appropriate
- Architecture/complexity — do you want all processing to be done client side or can you afford the effort (cost) to manage a server side implementation?
- 代码复杂性——如果你已经有了数据结构的代码,那么 Web Workers 可能很适合,否则 IndexedDB 看起来更合理。
- 性能——如果你需要一致的性能,服务器端实现或数据库似乎更合适
- 架构/复杂性——您希望所有处理都在客户端完成,还是您能负担得起管理服务器端实现的努力(成本)?
I found this booka useful read.
我发现这本书很有用。