gpt4 book ai didi

javascript - 在没有 I/O 的情况下,javascript(在浏览器中)的异步/ promise 是否有益?

转载 作者:数据小太阳 更新时间:2023-10-29 04:21:47 25 4
gpt4 key购买 nike

我正在尝试为 json-api 找到一个好的反序列化器/反规范化器(事实证明这非常困难)。

我遇到过几个示例,其中反序列化过程(基本上只是对关系进行反规范化和扁平化属性)被定义为异步函数。 Here's one such example ,但我发现了很多。

现在,我对 node/javascript 的理解是它基于受 I/O 限制的系统,因此它的设计是操作应该是非阻塞的,以便其他操作可以在 I/O 期间进行调度,因此我们获取并发操作。

然而,我不明白的是像这样的反序列化器中的用法。我们在反序列化时拥有完整的有效负载,没有任何 I/O 发生。我只能猜测作者假设关系查找可以同时发生,但是,由于 javascript 仍然是单线程的,我看不出这如何以任何方式提高性能。

在我看来,这只是使确定性操作成为非确定性操作(因为我认为调度还可以安排除反序列化之外的其他操作)。

我是不是漏掉了什么?使这个异步真的有好处吗?我不是前端(或 Node )开发人员,所以我觉得我错过了一些东西(因为我已经看到这种模式在反序列化器中使用了很多)

如果有任何不同,这将在浏览器(而不是 Node 后端)中运行。

最佳答案

看起来,您作为示例提到的库的作者没有正确使用 async/await:

 // see: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/src/deattribute/index.js

// v does await nothing, as it receives an array
// v unneccessary
await data.map(async el => deattribute(el))

这里根本没有理由使用 async/await,所以我怀疑它在库中是否有任何用途。

Are async/promises in javascript (in the browser) beneficial where there's no I/O?

没有。尽管 promises 总是异步解决的,它们仍然会在所谓的微任务队列中结束(在浏览器中),在浏览器重新渲染之前它会被清空,因此调用异步函数不会帮助您解冻 UI。

I can only guess that the author assumes that the relationship lookups could all happen concurrently, however, since javascript is still single threaded, I can't see how this could in any way improve performance.

我同意,这根本不会提高性能。

It seems to me this is just making a deterministic operation non deterministic (since I suppose the schedule could also schedule other operations besides the deserialization).

不,它不能,因为不涉及可以将引擎从当前任务中解放出来的异步 IO。因此它仍然会以阻塞的方式运行。

关于javascript - 在没有 I/O 的情况下,javascript(在浏览器中)的异步/ promise 是否有益?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55358764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com