请考虑以下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.
更多推荐
发布评论