48
ALWAYS JS Brendan Eich Brave Software @BrendanEich Always bet on

Always bet on JS - Finjs.io NYC 2016

Embed Size (px)

Citation preview

Page 1: Always bet on JS - Finjs.io NYC 2016

ALWAYSJS

Brendan EichBrave Software@BrendanEich Always bet on

Page 2: Always bet on JS - Finjs.io NYC 2016

ALWAYSBrendan Eich Brave Software @BrendanEich Always bet on JS

There are only two kinds of languages: the ones people complain about and the ones nobody uses. 

— Bjarne Stroustrup, C++ creator Managing Director, Techology; Morgan Stanley

Page 3: Always bet on JS - Finjs.io NYC 2016
Page 4: Always bet on JS - Finjs.io NYC 2016
Page 5: Always bet on JS - Finjs.io NYC 2016
Page 6: Always bet on JS - Finjs.io NYC 2016
Page 7: Always bet on JS - Finjs.io NYC 2016
Page 8: Always bet on JS - Finjs.io NYC 2016
Page 9: Always bet on JS - Finjs.io NYC 2016
Page 10: Always bet on JS - Finjs.io NYC 2016
Page 11: Always bet on JS - Finjs.io NYC 2016
Page 12: Always bet on JS - Finjs.io NYC 2016
Page 13: Always bet on JS - Finjs.io NYC 2016
Page 14: Always bet on JS - Finjs.io NYC 2016
Page 15: Always bet on JS - Finjs.io NYC 2016
Page 16: Always bet on JS - Finjs.io NYC 2016

JS didn’t always have it this good…

Page 17: Always bet on JS - Finjs.io NYC 2016
Page 18: Always bet on JS - Finjs.io NYC 2016

For a Better Web

Enterprise Browser Challenge:Secure the TLS (https:) Wormhole

(Get Brave now at https://brave.com)

Page 19: Always bet on JS - Finjs.io NYC 2016

For a Better Web

Typical tracking on a popular site Same site with Brave

(Map data from Ghostery)

www.tmz.com

Google Analytics

Google Fonts

II-assets.tmz.com

II-media.tmz.com

ssl-cdn-media.tmz.com

ssl-cdn-assests.tmz.com

(simulated map data)

Cross-site Images + Cookies + JS Created a Monster

Page 20: Always bet on JS - Finjs.io NYC 2016

For a Better Web

Typical Web Browsers

Typical Enterprise Web Access

Sensitive Business Data

Malware

500K Unprotected Employees

Clean web traffic

Spyware

Ad Tracking

Malicious Ads

Internet HTTP/HTTPS

Typical Companies

Page 21: Always bet on JS - Finjs.io NYC 2016

For a Better Web

Brave Secure Browser Approach

Brave for Secure Enterprise Web Access

500K Safe Employees

Clean web traffic

Internet HTTP/HTTPS

whitelist sites

othersites

Malware

Spyware

Ad Tracking

Malicious Ads

Brave Page Filter Browser Extension Brave

Browser

Page 22: Always bet on JS - Finjs.io NYC 2016

1995-era JS in Ideal Browser (w/ 2016 HTML5)

Page 23: Always bet on JS - Finjs.io NYC 2016

Fast-forward 21 years

Page 24: Always bet on JS - Finjs.io NYC 2016
Page 25: Always bet on JS - Finjs.io NYC 2016

(Restarting JS standards: a Herculean task)

Page 26: Always bet on JS - Finjs.io NYC 2016

2016-era JS VM + Modern Browser Architecture

Page 27: Always bet on JS - Finjs.io NYC 2016

Open Web Standards, Multiple Implementations

Page 28: Always bet on JS - Finjs.io NYC 2016

Now let’s add WebAssembly

Page 29: Always bet on JS - Finjs.io NYC 2016

WebAssembly: JS VM 2nd Input Language

Page 30: Always bet on JS - Finjs.io NYC 2016

Q: So what are WebAssembly APIs?

A: Web APIs!

Page 31: Always bet on JS - Finjs.io NYC 2016

This is a key difference from plugin architectures

Page 32: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

What is WebAssembly? The Short Form• A new standard being developed in a W3C Community Group and

on GitHub with Apple, Google, Microsoft and Mozilla which defines:

• 1. Input: a compact, portable binary format which is fast to load and runs safely at predictably near-native speed

• 2. Output: a 1:1 text format rendered by developer tools for “View Source” prettyprinting

Page 33: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

What is WebAssembly? Multiple Choice• Depending on where you are coming from:

• 1. Compiler target for the Web

• 2. Virtual ISA

• 3. Yet another JavaScript feature

• 4. Evolution of asm.js

Page 34: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

1. Compiler target for the Web• Not a programming language per se

• Compile from programming languages to WebAssembly

Page 35: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

1. Compiler target for the Web• Optimized for size and compile speed, not for direct execution, so it looks different from

traditional assembly language • typed stack machine

• save bytes on register operands • single-def-use analysis for free

• constrained to postorder AST serialization • validate = AST type-checking (simple and fast) • render to expression trees in View Source

• structured control flow via stack of branch targets • fast single-pass SSA construction • simplifies various other graph algorithms

Page 36: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

2. Virtual Instruction Set Architecture (ISA)• As close to physical machine instructions as safety and portability

allow ("check" can safely eliminated on 64-bit, hoistable otherwise)

WebAssembly x86 ARM

i32.add add ADD

call call BL

i32.load check + mov check + LDR

Page 37: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

3. Yet Another JavaScript Feature• ability to efficiently load and cache large code

• + predictable near-native performance

• = powerful library-building tool

Page 38: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

3. Yet Another JavaScript Feature• What use does JS have for compiled libraries?

• We see all these today via C++ compiled to JS:

• client-side image compression/filters, mapping, language detection, computer vision including facial recognition (via getUserMedia), crypto, compression, audio mixing, ...

• And that's before considering games/simulations

Page 39: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

4. Evolution: what is asm.js?• asm.js is an extraordinarily optimizable, low-level subset of

JavaScript that can be compiled from languages like C/C++

• Because it's just JavaScript, it runs well on all browsers today

• Currently, Firefox and Edge ship special asm.js optimizations

• Chrome has asm.js optimizations in development

Page 40: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

4. Evolution: how do you get asm.js?

• Compile pipeline: C/C++ ⇒ clang/LLVM ⇒ Emscripten ⇒ asm.js

• emcc(1) command; most clang options work, as do gcc options

Page 41: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

4. Evolution: what does asm.js look like?

Brendan Eich Brave Software @ BrendanEich

function asmModule(global, imports, buffer) { // module pattern "use asm"; // no-op hint var print = imports.print; // module imports var heap32 = new global.Int32Array(buffer); // C++ heap ... function foo(ptr) { // C/C++ functions ⇒ JS functions ptr = ptr|0; // Coercions declare arg types var tmp = 0; // Initializer declares var type tmp = heap32[ptr>>2]|0; // Pointers ⇒ integers tmp = (tmp + 1)|0; // Guaranteed 1 add instruction print(tmp|0); // Call out to non-asm.js } ... return {foo:foo, ...}; // Exports called from non-asm.js }

Allows AOT, parallel compilation and code caching (blog)

Page 42: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

4. Evolution: proposed asm.js extensions• Shared memory multithreading: SharedArrayBuffer

• Single-Instruction/Multiple-Data parallelism: SIMD.js

• Experimental support in Firefox (Nightly) and Emscripten

Page 44: Always bet on JS - Finjs.io NYC 2016

4. Evolution: from asm.js to WebAssembly• Can think of WebAssembly as a binary encoding of asm.js with

tweaks to make it a better compiler target

asm.js WebAssembly

(x+y)|0 i32.add

emulate (JS needs int64) i64.add

f(x|0)|0 call $f

HEAP32[i>>2]|0 i32.load

Page 45: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

New Numeric Types for AI/FinTech/etc. JS• Int64(0) ==> 0L // as in C#

• Uint64(0) ==> 0UL // ditto

• Float32(0) ==> 0f // ditto

• BigInt(0) ==> 0N // N to avoid i/I

• Decimal(0) ==> 0m // m or M, C/F#

• User-defined suffixes and operators: | ^ & == < <= << >> >>> + - * / % ~ ToBoolean unary- unary+

Page 46: Always bet on JS - Finjs.io NYC 2016

ALWAYSAlways bet onBrendan Eich Brave Software @ BrendanEich

JS

Will WebAssembly Replace JavaScript?• No, I don't think so. Why?

• JS has huge & growing momentum, vibrant ecosystem, stickiness

• JS in full remains the one obligate built-in language of the Web

• Web APIs continue to be designed specifically for JS first

• In fact, WebAssembly may have quite the opposite effect:

• If you're going to target WebAssembly and your app needs a scripting language, JS is the obvious first choice...

Page 47: Always bet on JS - Finjs.io NYC 2016

Demos

Page 48: Always bet on JS - Finjs.io NYC 2016