是否可以使用 Web Worker 优化两个脚本? [英] Is it possible to optimize two scripts using web workers?

查看:32
本文介绍了是否可以使用 Web Worker 优化两个脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行两个具有依赖性的库.我的意思是它们的执行顺序无关紧要.它们会相互检测并以任意顺序运行.

I'm running two libraries that are dependency aware. What I mean is the order of their execution does not matter. They will detect each other and run in any order.

script1.js

script2.js

使用 Web Worker 并行运行这些脚本(并行处理)是否会让我受益?(MDN 文档)

Would I benefit by running these scripts in parallel (parallel processing) using web workers? (MDN Doc)

我的意思是它们会比我连续运行它们运行得更快吗?

I mean would they run faster than if I just ran them serially?

这两个脚本都已在客户端上可用(我不必下载它们).

Both scripts are already available on the client ( I don't have to download them ).

推荐答案

如果一个或两个脚本是纯函数式的,这意味着它不需要访问 DOM 或任何全局 Javascript 对象,那么您可以受益来自使用 Web Workers.

If one or both of the scripts is/are purely functional, meaning it doesn't need to access the DOM or any global Javascript objects, then you could benefit from using Web Workers.

如果不是这样,那么 Web Workers 对你没有好处.当 JS 通过 web worker 运行时,它只能将消息传递回主 DOM 线程并接收它们.

If this is not the case, then Web Workers will do you no good. When JS is run through a web worker, it can only pass messages back to the main DOM thread and receive them.

这篇关于是否可以使用 Web Worker 优化两个脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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