gpt4 book ai didi

javascript - 为什么 let 不比 var 慢?

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

做一个极端的总结,the difference between var and let他们的生活在一个范围内。

因此,如果我们要以 this answer 中的示例为例:

(function() {
for (var i = 0; i < 5; i++) {
setTimeout(function() {
console.log(`i: ${i}`);
}, i * 100);
}
// 5, 5, 5, 5, 5


for (let j = 0; j < 5; j++) {
setTimeout(function() {
console.log(`j: ${j}`);
}, 1000 + j * 100);
}
// 0, 1, 2, 3, 4
}());

  • i(用 var 声明)存在于整个 function
  • j(用 let 声明)只存在于 for 循环中。

对我来说,这意味着 javascript,在每次迭代之后,除了声明和分配给变量之外,在 let 的情况下,它还需要执行一个额外的步骤:清理 j

但如果我正在阅读 the specs对了,还有很多:

  1. 对于 var,执行以下步骤:

IterationStatement : for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement

  1. If the first Expression is present, then
    • Let exprRef be the result of evaluating the first Expression.
    • Let exprValue be GetValue(exprRef).
    • ReturnIfAbrupt(exprValue).
  2. Return ForBodyEvaluation(the second Expression, the third Expression, Statement, « », labelSet).
  1. 但在 let 的情况下,执行了多达 12 个步骤的 list ,包括创建新的声明性环境。

    IterationStatement : for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement

    1. Let oldEnv be the running execution context’s LexicalEnvironment.
    2. Let loopEnv be NewDeclarativeEnvironment(oldEnv).
    3. Let isConst be the result of performing IsConstantDeclaration of > 1. LexicalDeclaration.
    4. Let boundNames be the BoundNames of LexicalDeclaration.
    5. For each element dn of boundNames do
      • If isConst is true, then
        • Perform loopEnv.CreateImmutableBinding(dn, true).
      • Else,
        • Perform loopEnv.CreateMutableBinding(dn, false).
        • Assert: The above call to CreateMutableBinding will never return an abrupt completion.
    6. Set the running execution context’s LexicalEnvironment to loopEnv.
    7. Let forDcl be the result of evaluating LexicalDeclaration.
    8. If forDcl is an abrupt completion, then
      • Set the running execution context’s LexicalEnvironment to oldEnv.
      • Return Completion(forDcl).
    9. If isConst is false, let perIterationLets be boundNames otherwise let perIterationLets be « ».
    10. Let bodyResult be ForBodyEvaluation(the first Expression, the second Expression, Statement, perIterationLets, labelSet).
    11. Set the running execution context’s LexicalEnvironment to oldEnv.
    12. Return Completion(bodyResult).

那么为什么在运行以下测试(我从 the same question I previously referenced 中获取)时,var 的执行速度比 let 慢,反之亦然,因为我怀孕了吗?

(function() {
let varTime = performance.now()
for (var i = 0; i < 100000000; i++) {}
varTime = performance.now() - varTime;
console.log('var', varTime)


let letTime = performance.now()
for (let i = 0; i < 100000000; i++) {}
letTime = performance.now() - letTime;
console.log('let', letTime)
}).call({});

TEST 1
var: 147.500ms
let: 138.200ms

TEST 2
var: 141.600ms
let: 127.100ms

TEST 3
var: 147.600ms
let: 122.200ms

最佳答案

我同意@Derek 绅会功夫的观点,如果不深入实现,真的很难推理。

但是,对于 var,您可能错过了一些步骤:

var 的情况下,实际上执行了更多 个步骤:

IterationStatement : for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement

  1. If the first Expression is present, then
    • Let exprRef be the result of evaluating the first Expression.
    • Let exprValue be GetValue(exprRef).
      • ReturnIfAbrupt(V).
      • If Type(V) is not Reference, return V.
      • Let base be GetBase(V).
      • If IsUnresolvableReference(V), throw a ReferenceError exception.
      • If IsPropertyReference(V), then
        • If HasPrimitiveBase(V) is true, then
          • Assert: In this case, base will never be null or undefined.
          • Let base be ToObject(base).
        • Return base.[[Get]](GetReferencedName(V), GetThisValue(V)).
      • Else base must be an Environment Record,
        • Return base.GetBindingValue(GetReferencedName(V), IsStrictReference(V)) (see 8.1.1).
    • ReturnIfAbrupt(exprValue).
  2. Return ForBodyEvaluation(the second Expression, the third Expression, Statement, « », labelSet).

稍微 额外的处理可能来自 GetValue .

关于javascript - 为什么 let 不比 var 慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51531675/

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