4 ms·
I'm curious to see if they broke any code, like the IE engine did recently. For example, with loop-invariant code motion, what is legal in a language like C, m
by kenjackson 16y ago
I'm curious to see if they broke any code, like the IE engine did recently. For example, with loop-invariant code motion, what is legal in a language like C, may not be in Javascript (for same reason the IE DCE optimization was invalid).
I'd find it hard to believe that Goog would make the same mistake after all the hullaballoo, but I'd love to see it validated.
- pbiggar 16y agoLoop-invariant code motion is still legal in JS, and most dynamic languages, as is DCE. You just need to take into account the weird semantics of JS. What the IE team got wrong was the assumptions they made about the semantics of the code that was optimized. If they had checked for the presence of a valueOf property, they could still have done the optimization.
- kenjackson 16y agoExactly. I'm not saying those opts aren't legal, but in cases that look legal in C, they're not legal in JS. And note, you can still do it in the presence of a custom valueOf method, as long as it doesn't have side effects (and thus of course loop-invariant itself). In essence I'm asking if Google does in fact do this check, and does analysis to ensure that the valueOf method doesn't get written dynamically in the loop itself.
- VMG 16y agoI'd like to have a guide for writing code that is easily optimized by V8 and similar engines. Having local variables is good, as far as I can tell, but it would be nice to have a full overview with dos and don'ts
- drivebyacct2 16y ago"Having local variables is good" Reading sentences like this scare me because it reminds me that some people don't know what the 'var' keyword means or think it's acceptable to shove everything in window or global.
- ams6110 16y agoLast I read, JavaScript has 2 scopes: function scope, and global scope. Unlike many languages, there is no block scope (e.g. a counter in a for loop).
- carussell 16y agoThere is in Mozilla https://developer.mozilla.org/en/New_in_JavaScript_1.7#let_statement https://developer.mozilla.org/en/New_in_JavaScript_1.7#let_s... http://wiki.ecmascript.org/doku.php?id=proposals:block_expressions&s=let http://wiki.ecmascript.org/doku.php?id=proposals:block_expre...
- solutionyogi 16y agoIt's definitely not available in current JavaScript interpreter in different browsers.
- drivebyacct2 16y agoI feel dumb now because I fear I don't understand what you mean when you say block scope... for(var i=0; i<3; i++) { console.log(i); } appears to be valid... (or just in Chrome?)
- aboodman 16y agoValid, just doesn't do what you think. function foo() { var x = 1; if (true) { var x = 2; var y = 3; } console.log(x); console.log(y); } foo(); // 2, 3 (vs 1, undefined if there was block scope)
- drivebyacct2 16y agoThanks for the explanation!
- pornel 16y agoJS performs "variable hoisting", so your example is interpreted as: function(){ var i; … for(i=0; … It has surprising side effects: alert(i); var i; works and outputs "undefined", which is value of the variable i that is defined. alert(k); var i; This is an error, and will fail to execute because of undefined variable.
- alanh 16y agoThe IE team didn’t break anything in shipping software, right? This is just an IE9 beta problem, presumably fixable before they ship a stable IE9?
- kenjackson 16y agoAbsolutely. It wasn't even beta, it was just a tech preview. If they didn't have bugs of that sort, they aren't pushing things hard enough.