Slide 1

Slide 1 text

StrongMode & SoundScript @yosuke_furukawa

Slide 2

Slide 2 text

StrictMode

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

make your code strict AND those rules can prevent some mistakes

Slide 5

Slide 5 text

In TC39, Google v8 developers suggested more stricter mode.

Slide 6

Slide 6 text

Strong Mode

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Demo

Slide 10

Slide 10 text

“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

Slide 11

Slide 11 text

“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

Slide 12

Slide 12 text

Predictability

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

“use strong” kills Optimizer killer

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Not now. please wait.

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

SoundScript

Slide 20

Slide 20 text

Not Implemented yet.

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Conclusion

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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/