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. “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; } }
  2. “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 }
  3. “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
  4. “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
  5. 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 !!!!!!
  6. 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
  7. 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
  8. SoundScript will be/have … • Based on TypeScript • interop

    with old code • Aggressive optimisations • Optional Types
  9. 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
  10. 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….
  11. 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/