是“让x = 0;(Are “let x = 0; x += 1” and “let x = 0; x = x + 1” semantically different?)

编程入门 行业动态 更新时间:2024-10-28 12:30:14
是“让x = 0;(Are “let x = 0; x += 1” and “let x = 0; x = x + 1” semantically different?)

请考虑以下javascript代码段:

let x = 0; x += 1;

let x = 0; x = x + 1;

这两个片段之间是否存在任何有意义的差异? 我问的原因是因为第一个示例阻止Chrome优化包含它的函数,而第二个示例则没有。 该基准测试说明了许多迭代的性能差异。

至于我原来的问题,我想知道是否存在任何语义差异,因为如果没有,我想知道为什么Chrome不会将复合赋值减去x = x + 1形式。

Consider the following javascript snippets:

let x = 0; x += 1;

and

let x = 0; x = x + 1;

Is there any meaningful differences between these two snippets? The reason I ask is because the first example prevents Chrome from optimizing the function containing it, while the second example doesn't. This benchmark illustrates the performance differences over many iterations.

As for my original question, I wonder if there is any semantic difference because if there isn't, I would like to know why Chrome wouldn't just de-sugar the compound assignment to the x = x + 1 form.

最满意答案

按照标准 - 它们在语义上是等价的。

运行时语义的相关部分:

让op为@ ,其中AssignmentOperator是@= 。 设r是将op应用于lval和rval的结果,就好像计算表达式lval op rval 。 表演? PutValue(lref, r) 。

这两个片段之间是否存在任何有意义的差异?

这些之间不应有任何可观察到的差异。

至于我原来的问题,我想知道是否存在任何语义差异,因为如果没有,我想知道为什么Chrome不会将复合赋值减去x = x + 1形式。

优化是一个复杂的主题。 let (最近相对)介绍并且尚未优化以及var 。

As per the standard - they are semantically equivalent.

Relevant part of the runtime semantics:

Let op be the @ where AssignmentOperator is @=. Let r be the result of applying op to lval and rval as if evaluating the expression lval op rval. Perform ? PutValue(lref, r).

Is there any meaningful differences between these two snippets?

There should not be any observable difference between those.

As for my original question, I wonder if there is any semantic difference because if there isn't, I would like to know why Chrome wouldn't just de-sugar the compound assignment to the x = x + 1 form.

Optimisation is a complicated subject. let was introduced (relatively) recently and is not optimised as well as var yet.

更多推荐

本文发布于:2023-08-05 21:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:semantically

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!