Upgrade to Pro — share decks privately, control downloads, hide ads and more …

StrongMode And SoundScript

StrongMode And SoundScript

ES2015 発刊記念勉強会の発表

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa
PRO

June 26, 2015
Tweet

Transcript

  1. StrongMode & SoundScript @yosuke_furukawa

  2. StrictMode

  3. “use strict” “use strict”; function foo() { abc = “def”;

    // throw TypeError when variables does not have `var` declaration. var obj1 = {}; Object.defineProperty(obj1, "x", { value: 42, writable: false }); obj1.x = 9; // throw TypeError when you substitute unwritable variable with (obj1) { // Restrict `with` x; } }
  4. make your code strict AND those rules can prevent some

    mistakes
  5. In TC39, Google v8 developers suggested more stricter mode.

  6. Strong Mode

  7. StrongMode Motivation • Usability especially `maintainability` • Performance especially `predictability`

    • embrace ES2015 • avoid bad legacy codes • (AND types)
  8. “use strong” “use strong”; function foo() { // v8 4.2

    enabled (io.js v2.0 also) var obj1 = ‘aaa’; // Restrict var (use let or const instead) let args = arguments; // Restrict arguments (use Rest Params …args) if (obj1 == ‘aaa’) {} // Restrict == (use strict equal ===) if (obj1 === ‘aaa’); // Restrict empty if and for for (let n in [1,2,3]) // Restrict for-in (use for-of instead) delete obj1.key // Restrict delete operator (use Map/Set instead) // v8 4.5 enabled TODO: try let undefined = ‘aaa’; // Restrict undefined binding let obj3 = { foo : ‘aaa’}; obj3.bar = ‘123’; // Restrict undefined property access }
  9. Demo

  10. “use strong” “use strong”; function foo() { // v8 4.2

    enabled (io.js v2.0 also) var obj1 = ‘aaa’; // Restrict var (use let or const instead) let args = arguments; // Restrict arguments (use Rest Params …args) if (obj1 == ‘aaa’) {} // Restrict == (use strict equal ===) if (obj1 === ‘aaa’); // Restrict empty if and for for (let n in [1,2,3]) // Restrict for-in (use for-of instead) delete obj1.key // Restrict delete operator (use Map/Set instead) // v8 4.5 enabled TODO: try let undefined = ‘aaa’; // Restrict undefined binding let obj3 = { foo : ‘aaa’}; obj3.bar = ‘123’; // Restrict undefined property access } 6TBCJMJUZ&4
  11. “use strong” “use strong”; function foo() { // v8 4.2

    enabled (io.js v2.x also) var obj1 = ‘aaa’; // Restrict var (use let or const instead) let args = arguments; // Restrict arguments (use Rest Params …args) if (obj1 == ‘aaa’) {} // Restrict == (use strict equal ===) if (obj1 === ‘aaa’); // Restrict empty if and for for (let n in [1,2,3]) // Restrict for-in (use for-of instead) delete obj1.key // Restrict delete operator (use Map/Set instead) // v8 4.5 enabled TODO: try let undefined = ‘aaa’; // Restrict undefined binding let obj3 = { foo : ‘aaa’}; obj3.bar = ‘123’; // Restrict undefined property access } 6TBCJMJUZ&4 1FSG1SFEJDUBCJMJUZ
  12. Predictability

  13. Optimizer Killer - 
 property assign/delete function Point(x, y) {

    this.x = x; this.y = y; } let p1 = new Point(1, 2); let p2 = new Point(3, 4); p2.name = “test”; // <—- Optimizer Killer(eliminate hidden class) !!!!!!! delete p1.x // <—- Optimizer Killer !!!!!!
  14. Optimizer Killer - arguments function assignToArgs() { arguments = 3

    // <— Optimizer Killer !!!! return arguments; } function leaksArguments2() { var args = [].slice.call(arguments); // <— Optimizer Killer !!!! return args; } // Actually, arguments should not be assigned any params and should never use arguments directly !!!!! // arguments.lenght, arguments[i] are better. // see: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3- managing-arguments
  15. “use strong” kills Optimizer killer

  16. Should we use “use strong” at the moment???

  17. Not now. please wait.

  18. Reason • Experimental feature • ES6 code is not so

    faster than ES5 code. • v8 optimizer is not mature. • ScalaJS author measures the performance between ES5 and ES6 and StrongMode • https://groups.google.com/forum/?hl=ja#!topic/ strengthen-js/Bfa_HPhVbiY
  19. SoundScript

  20. Not Implemented yet.

  21. SoundScript will be/have … • Based on TypeScript • interop

    with old code • Aggressive optimisations • Optional Types
  22. SoundScript “use stricter+types”; // use stricter seals every class and

    instance class Point { constructor(x : int, y : int) { // aggressive optimize this.x = x; this.y = y; } move(dx : int, dy : int) : void { ··· } } function norm(p : Point) : double { return Math.sqrt(p.x*p.x + p.y*p.y); } http://www.2ality.com/2015/02/soundscript.html
  23. Conclusion

  24. Conclusion • StrongMode is more stricter than StrictMode. • StrongMode

    is Optimizer friendly. • SoundScript looks like TypeScript, flow type • But SoundScript gives optimizer hints • The Future Typed JavaScript looks bright….
  25. Reference • https://esdiscuss.org/notes/2015-01/JSExperimentalDirections.pdf • https://developers.google.com/v8/experiments • https://github.com/iojs/io.js/pull/933#discussion_r26895522 • http://incaseofstairs.com/2015/06/es6-feature-performance/ •

    https://github.com/v8/v8-git-mirror/blob/ 85dbfb9a389e7b21bd2a63862202ee97fc5d7982/src/messages.h • https://github.com/petkaantonov/bluebird/wiki/Optimization-killers • http://efcl.info/2014/09/13/v8-optimization-memo/ • http://mrale.ph/irhydra/2/