JavaScript 中的并行处理模拟

发布于 2024-10-16 07:05:33 字数 275 浏览 3 评论 0原文

我是 JavaScript 新手,请原谅我是个菜鸟。

当需要密集计算时,它很可能涉及递归或其他循环。有时,这可能意味着有一个运行四个函数的递归循环,并且可能每个函数都会遍历整个 DOM 树,读取位置并进行一些数学运算以进行碰撞检测或其他操作。

当第一个函数正在遍历 DOM 树时,下一个函数必须等待第一个函数完成,依此类推。与其这样做,为什么不在程序外部单独启动这些循环中的循环,并在另一个运行较慢的循环中执行计算,因为它本身不执行这些计算?

是智障还是聪明?

提前致谢!

I'm new to JavaScript so forgive me for being a n00b.

When there's intensive calculation required, it more than likely involves loops that are recursive or otherwise. Sometimes this may mean having am recursive loop that runs four functions and maybe each of those functions walks the entire DOM tree, read positions and do some math for collision detection or whatever.

While the first function is walking the DOM tree, the next one will have to wait its for the first one to finish, and so forth. Instead of doing this, why not launch those loops-within-loops separately, outside the programs, and act on their calculations in another loop that runs slower because it isn't doing those calculations itself?

Retarded or clever?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆头 2024-10-23 07:05:33

长期计算正是 Web Workers 的用途。您所描述的是生产者和/或消费者线程的常见模式。虽然您可以使用 Web Workers 来完成此操作,但即使在高度并行的系统上,同步开销也可能会超过任何收益。

JavaScript 并不是计算要求较高的应用程序的理想语言。此外,网络浏览器机器的处理能力可能差异很大(想想低端智能手机与 16 核工作站)。因此,请考虑在服务器上计算复杂的内容并将结果发送到客户端进行显示。

对于您的日常 Web 应用程序,您应该采用单线程方法,并在出现问题时分析性能。哎呀,为什么不在这里寻求有关性能问题的帮助呢?

Long-term computations are exactly what Web Workers are for. What you describe is the common pattern of producer and/or consumer threads. While you could do this using Web Workers, the synchronization overhead would likely trump any gains even on highly parallel systems.

JavaScript is not the ideal language for computationally demanding applications. Also, processing power of web browser machines can vary wildly (think a low-end smartphone vs. a 16core workstation). Therefore, consider calculating complex stuff on the server and sending the result to the client to display.

For your everyday web application, you should take a single-threaded approach and analyze performance once it becomes a problem. Heck, why not ask for help about your performance problem here?

千と千尋 2024-10-23 07:05:33

JavaScript 从来就不是用来执行如此计算密集型任务的,尽管这种情况正在改变,但 JavaScript 本质上是单线程的事实仍然存在。最近的 Web Workers 技术提供了有限形式的多线程,但这些工作线程无法访问直接 DOM;他们只能向主线程发送/接收消息,然后主线程可以代表他们访问消息。

JavaScript was never meant to do perform such computationally intensive tasks, and even though this is changing, the fact remains that JavaScript is inherently single-threaded. The recent web workers technology provides a limited form of multi-threading but these worker threads can't access the DOM directly; they can only send/receive messages to the main thread which can then access it on their behalf.

终遇你 2024-10-23 07:05:33

Currently, the only way to have real parallel processing in JS is to use Web Workers, but it is only supported by very recent browsers. And if your program requires such a thing, it could mean that you are not using the right tools (for example, walking the DOM tree is generally done by using DOM selectors like querySelectorAll).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文