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

StrongMode And SoundScript

StrongMode And SoundScript

ES2015 発刊記念勉強会の発表

Yosuke Furukawa

June 26, 2015
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. StrongMode &
    SoundScript
    @yosuke_furukawa

    View full-size slide

  2. “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;
    }
    }

    View full-size slide

  3. make your code strict
    AND
    those rules can prevent
    some mistakes

    View full-size slide

  4. In TC39, Google v8
    developers suggested more
    stricter mode.

    View full-size slide

  5. StrongMode Motivation
    • Usability especially `maintainability`
    • Performance especially `predictability`
    • embrace ES2015
    • avoid bad legacy codes
    • (AND types)

    View full-size slide

  6. “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
    }

    View full-size slide

  7. “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

    View full-size slide

  8. “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

    View full-size slide

  9. Predictability

    View full-size slide

  10. 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 !!!!!!

    View full-size slide

  11. 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

    View full-size slide

  12. “use strong” kills
    Optimizer killer

    View full-size slide

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

    View full-size slide

  14. Not now.
    please wait.

    View full-size slide

  15. 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

    View full-size slide

  16. Not Implemented yet.

    View full-size slide

  17. SoundScript will be/have …
    • Based on TypeScript
    • interop with old code
    • Aggressive optimisations
    • Optional Types

    View full-size slide

  18. 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

    View full-size slide

  19. 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….

    View full-size slide

  20. 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/

    View full-size slide