chakra-core / chakra-core/ChakraCore
`let` vs `var` generates a lot more instructions
- Dominant language
- JavaScript
- Stars
- 9.3k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
Sorry in advance I haven't been able to get a simpler repro case at this time.
My test case is the following
``` js
const n = 500;
function getTest(name) {
var fn;
eval(`fn = function memcopy_${name}(a, b, start, end) {for (let i = start; i < end; i++) { b[i] = a[i]; }}`);
return fn
}
var foo = getTest("Int8")
var src = new Int8Array(n);
src.fill(1);
var dst = new Int8Array(n);
foo(src, dst, 0, 250);
foo(src, dst, 250, n);
```
This code generates the following IR
```
Function Entry
s7.var = NewBlockScope #0000
s15(s7->i).var = InitLetFld 0xXXXXXXXX (null)[PrimitiveOrObject].var #0002
Every uses of `i`
s19.var = LdSlotArr s17(s7[2]).var #0017
s8.var = LdSlot s20(s19[0]).var #0017
```
This IR makes it hard to make proper optimizations (in this case Array optimizations).
If we change the `let` keyword for a `var`, `i` becomes a regular variable.
Is it possible to make `let` variables behave exactly the same as a regular `var` if there is no redefinition inside the function ?
Should this be done at Bytecode level, IR Builder or Globopt ?
Contributor guide
Assessment
This issue has not been assessed yet.