News for hackers. Stuff that matters.
Re:V8 has optimized new JavaScript language features (2018) by rewq4321 on Saturday November 07, 2020 @03:10PM (#25018689)
JavaScript is very dynamic. Here I define a variable with `let` and then change it:

let foo = 10; eval("fo"+"o = 10");

I could "obfuscate" that eval assignment as much as I like. So you can't completely statically analyse `let` variables.

That said, it's likely you'd end up with perf very close to `const` in a very "hot" part of your code, since a good JIT compiler like V8 will eventually make "assumptions" about your code, and optimise around them, while having (ideally cheap) checks in place to ensure the assumptions continue to hold.

Re:V8 has optimized new JavaScript language features (2018) by jlokier on Saturday November 07, 2020 @03:30PM (#25018944)
An interesting point, however direct "eval" is a special case.

It's already known that functions containing direct "eval" are not subject to the same level of performance optimisations as other functions. There is no way to obscure the call to direct "eval" itself; the compiler knows clearly whether it occurs.

Without "eval" appearing syntactically inside a function's scope, there is no dynamic access to "let" variables, and there's no need for the JIT code to check the assumption at run time.

Despite no other assignments, the "let" variable does change value: It has the assigned value after the "let", and the "temporal dead zone" value before it in the same scope. However "const" also has this property so it's not obvious why there would be a speed difference.

Re:V8 has optimized new JavaScript language features (2018) by rewq4321 on Monday November 09, 2020 @04:42PM (#25040314)
> There is no way to obscure the call to direct "eval" itself; the compiler knows clearly whether it occurs.

I must be misunderstanding you, because you've stated that very confidently.

window["e"+"val"](...)

Re:V8 has optimized new JavaScript language features (2018) by syrrim on Saturday November 07, 2020 @06:12PM (#25020689)
> There is no way to obscure the call to direct "eval" itself;

Jsfuck would like to have a word with you

Re:V8 has optimized new JavaScript language features (2018) by jlokier on Saturday November 07, 2020 @08:48PM (#25022008)
Nice try! The Jsfuck method can't encode a direct "eval", only an indirect one :-)

Thus the emphasis on direct. Well, Jsfuck can encode a direct eval inside an indirect eval, but that doesn't give it any advantages, it still can't access the surrounding lexical environment in the form of "let" and "const" variables.

(I didn't know about Jsfuck though - it looks fun, thanks!)

Re:V8 has optimized new JavaScript language features (2018) by ithkuil on Saturday November 07, 2020 @06:42PM (#25020980)
Does it actually behave like having a literal eval in the function?
Re:V8 has optimized new JavaScript language features (2018) by Mavvie on Saturday November 07, 2020 @06:38PM (#25020947)
Doesn't JSFuck only work when strict mode is off?
Re:V8 has optimized new JavaScript language features (2018) by chrisseaton on Saturday November 07, 2020 @03:17PM (#25018791)
These checks can be completely free, in terms of runtime cost, through deoptimisation.
Re:V8 has optimized new JavaScript language features (2018) by gfxgirl on Saturday November 07, 2020 @08:29PM (#25021897)
How? You have to do some kind of check with let, and you don't with const.
Re:V8 has optimized new JavaScript language features (2018) by miloignis on Sunday November 08, 2020 @02:31AM (#25023875)
Not sure if V8 does this, but a general strategy used in HotSpot is to compile assuming the good case and install a "trap" to deoptimize the code if the condition is violated. So you could compile the let like a const with no checks on the use of the variable, but have a trap such that if any code ever tries to assign to that variable, all code compiled under the assumption that it was constant is throw out.