-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
19244 lines (17808 loc) · 867 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>The JavaScript Binary AST</title>
<style>
* {
.box-sizing(border-box);
}
*:before,
*:after {
.box-sizing(border-box);
}
html {
font-size: 100%;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
body {
margin: 0;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 24px;
line-height: 1.5;
color: #555555;
background-color: #FFFFFF;
}
a {
color: #80a000;
text-decoration: none;
font-weight: 500;
}
a:hover, a:focus {
color: #576d00;
text-decoration: underline;
}
a:focus {
outline: 3px solid #80a000;
outline-offset: 0;
background-color: #80a000;
color: #FFF;
text-decoration: none;
}
ol {
list-style-type: decimal;
padding: 0 0 0 1.5em;
margin: 1em 0;
}
ul {
list-style-type: none;
margin: 1em 0;
padding: 0;
}
li p {
display:inline;
}
li ul {
margin-top: 0em;
margin-left: 2em;
}
ul li:before {
content: "\25cf";
color: #00989e;
padding-right: 6px;
}
img {
vertical-align: middle;
max-height: 600px;
margin: 0 auto 1em;
display: block;
}
hr {
height: 0;
margin: 1em 0;
border: 0;
border-top: 1px solid #cccccc;
}
h1, h2, h3, h4, h5, h6 {
font-family: "graze-pro";
letter-spacing: -0.04em;
font-weight: 500;
margin: 1em 0 0.5em;
}
b,
strong {
font-weight: 500;
}
blockquote {
border-left: 5px solid #cccccc;
padding-left: 1em;
margin: 1em -2em;
font-style: italic;
color: #999999;
}
.remark-slide-number {
display: none;
}
.remark-slide {
display: table;
}
.remark-slide-content {
display: table-cell;
vertical-align: middle;
padding: 0 6em;
border-bottom: 18px solid black;
font-size: 24px;
}
.remark-slide-content:after {
content: "";
position: absolute;
bottom: 10px;
right: 0px;
height: 40px;
width: 120px;
background-repeat: no-repeat;
background-size: contain;
background-image: url('img/mozilla.svg');
}
.remark-slide-content h1 {
font-size: 56px;
color: #00989e;
}
.remark-slide-content h2 {
font-size: 48px;
color: #00989e;
}
.remark-slide-content h3 {
font-size: 40px;
}
.remark-slide-content h4 {
font-size: 36px;
}
.remark-slide-content h5 {
font-size: 30px;
}
.remark-slide-content h6 {
font-size: 24px;
}
.remark-slide-content > :first-child {
margin-top: 0;
}
.remark-slide-content > :last-child {
margin-bottom: 0;
}
.remark-inline-code {
background-color:rgb(245, 245, 245);
border: 1px inset black;
}
.remark-code-line {
white-space: pre;
background-color: #000;
border: 1px solid #000;
color: #fff;
padding: 0 1em;
}
.remark-code-line .hljs-string {
color: rgb(200, 110, 110)
}
.remark-code-line .hljs-keyword {
color: rgb(0, 255, 0)
}
.remark-code-line .hljs-literal {
color: rgb(155, 155, 0)
}
.remark-code-line .hljs-number {
color: rgb(150, 150, 255)
}
.mini img {
max-height: 2em;
display: inline;
}
.thatsimple img {
max-height: 300px;
}
.small {
font-size: small
}
.github {
position: absolute;
top: 0;
right: 0;
border: 0
}
/*
FILE ARCHIVED ON 13:47:02 Dec 06, 2016 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 22:18:20 Feb 02, 2018.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
*/
</style>
</head>
<body>
<textarea id="source">
# The JavaScript Binary AST
.center[A proposal to speed up the web]
.center[Mozilla, Bloomberg, Facebook]
.center[David Teller, Mozilla]
.center[Fosdem 2018]
---
## Making the web fast enough?
- On the web, there's no such thing as fast enough.
- JavaScript has become very fast but...
---
## ...looking at trends
- How much JS in Google Sheets, Google Docs, Yahoo!, LinkedIn, Facebook?
- 3-7 compressed Mb+ JS code.
--
- Updated *very* often.
- Sometimes every 5 minutes.
--
- Facebook: 500-900ms just *parsing* JavaScript
- Chrome & Firefox.
--
- And things are only getting worse.
---
![That's slow](img/slow.png)
---
## Why is JavaScript loading so slow?
---
### Loading JavaScript
1. Download source file.
1. Decompress source file.
1. Convert encoding.
1. Tokenize.
1. Parse.
1. Generate Bytecode.
1. Start execution.
---
![Would That It Were So Simple](img/wouldthatitweresosimple.gif)
---
### Step 1: Get the code
* Download and decompress source file.
* Convert encoding.
(typically two/three successive tasks)
---
### Step 2. Tokenize the text (1)
```js
function foo(x) {
return y;
}
```
.center[⇓]
* `Token.FunctionKeyword`
* `Token.Identifier(foo)`
* `Token.LPar`
* `Token.Identifier(x)`
* `Token.RPar`
* `Token.RBrace`
* ...
---
### Step 2. Tokenize the text (2)
Exercises:
* Is `for` an identifier or a keyword?
--
* What does `/` mean?
--
* Is `"use strict"` a string or a directive?
--
* How can I store my string efficiently?
--
* ...
--
**Hint** it depends.
--
Yeah, tokenization is hard. .mini[![Would That It Were So Simple](img/wouldthatitweresosimple.gif)]
---
### Step 3. Parse the tokens (1)
`[FunctionKeyword, Identifier(foo), ...]`
--
.center[⇓]
--
```yaml
FunctionDeclaration:
isAsync: false
isGenerator: false
scope: ...
name:
BindingIdentifier:
name: "foo"
params:
- FormalParameters:
items:
- BindingIdentifier:
name: "x"
rest: null
body: ...
```
---
### Step 3. Parse the tokens (2)
Exercise:
```javascript
var x = 10;
function foo(isReady) {
if (isReady) {
return x + 10;
}
// ...
}
```
What is the return of `foo(true)`?
--
**Hint** it depends.
---
### Step 3. Parse the tokens (3)
Parsing JS is hard:
```js
var x = 10;
function foo(isReady) {
if (isReady) {
return x + 10;
}
}
console.log(foo(true)); // `20`
var x = 10;
function foo(isReady) {
if (isReady) {
return x + 10;
}
var x = 10;
}
console.log(foo(true)); // `NaN`
```
---
### Step 3. Parse the tokens (4)
* ...handle variables .mini[![Would That It Were So Simple](img/wouldthatitweresosimple.gif)];
--
* ...handle `this` .mini[![Would That It Were So Simple](img/wouldthatitweresosimple.gif)];
--
* ...handle `eval` .mini[![Would That It Were So Simple](img/wouldthatitweresosimple.gif)];
--
* ...handle `with` .mini[![Would That It Were So Simple](img/wouldthatitweresosimple.gif)];
--
* ...handle `"use strict"` .mini[![Would That It Were So Simple](img/wouldthatitweresosimple.gif)];
--
* also, you can't skip anything;
---
### Step 4. Wait, there's more!
* Perform safety-checks on the code.
* Generate browser-specific bytecode.
* Execute!
---
## So what can we make faster?
1. Download source file.
1. Decompress source file.
1. Convert encoding.
1. Tokenize.
1. Parse.
1. Perform safety-checks.
1. Generate Bytecode.
1. Start execution.
---
### Things people have tried
* Browser improvements
* Lazy parsers (1)
* Bytecode caching (2)
* JavaScript frameworks/toolchains
* Minimizers (1)
* Lazy loaders (3)
* Browser APIs
* Wasm (3)
* ServiceWorker loaders (3, 4)
.small[
(1) May decrease global performance.
(2) When the JS of the page doesn't change.
(3) Requires rewrite. Might not help.
(4) Hurts the web.
]
---
## Introducing the JavaScript Binary AST
---
### What is it?
* A proposal for the JavaScript language, by Mozilla, Bloomberg, Facebook.
* A new file format for JavaScript code.
* Smaller than .js, much faster to parse.
* **Not** uglified.
* **Not** a bytecode.
---
### Recall parsing
```yaml
FunctionDeclaration:
isAsync: false
isGenerator: false
scope: ...
name:
BindingIdentifier:
name: "foo"
params:
- FormalParameters:
items:
- BindingIdentifier:
name: "x"
rest: null
body: ...
```
That's an Abstract Syntax Tree (AST).
---
### Storing the AST
```js
1: "foo",
[
/*FunctionDeclaration*/ 42,
/*isAsync*/0,
/*isGenerator*/0,
...,
/*name*//*BindingIdentifier*/ 43,
/*foo*/1,
...
]
```
(except compressed)
---
## What's the point?
---
### Things the browser needs to do.
1. Download binjs file.
1. Tokenize + Parse + Check **only what you use**.
1. Generate Bytecode.
1. Start execution.
---
### What is faster?
1. Smaller file (WIP).
1. Tokenization + Parsing can start earlier.
1. Trivial tokenization.
1. Format is **proof-carrying**.
* Hard cases are already processed.
* Checks are fast.
1. Parse **only** the code you execute, when needed.
1. Parse strings, identifiers, ... only **once**.
1. More opportunities for concurrency.
---
## Is there time for a demo?
---
## Calendar
---
### Early 2017
* Status: Proof of concept.
* Security: None.
* Speed: Extreme improvements (1).
* File size: Pretty good improvements (1).
* Standardization: Ecma step 1.
(1) No hard numbers because it's too early to make promises.
---
### Early 2018
* Status: Prototype 3.
* Security: As much as JavaScript, just easier to check.
* Reference implementation: ~90%.
* Firefox implementation: ~30%
* Speed: Not measured yet.
* File size: Improvements... but we want more.
* Specifications: High-level specifications ~90%.
---
### Hopefully late summer 2018
* Experimental deployment on a few major websites.
* Firefox implementation for opt-in users.
* Discussion with other browser vendors on further optimizations.
--
* Your help welcome :)
* https://github.com/binast/binjs-ref
* https://binast.github.io/ecmascript-binary-ast/
---
## Thanks for listening.
![It's that simple](img/simple.png)
[.github[![Fork me on github](img/github.png)]](https://github.com/Yoric/Fosdem-2018)
### Any questions?
</textarea>
<script>require=(function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({"components/printing":[function(require,module,exports){
module.exports=require('yoGRCZ');
},{}],"yoGRCZ":[function(require,module,exports){
var EventEmitter = require('events').EventEmitter
, styler = require('components/styler')
;
var LANDSCAPE = 'landscape'
, PORTRAIT = 'portrait'
, PAGE_HEIGHT = 681
, PAGE_WIDTH = 908
;
function PrintComponent () {}
// Add eventing
PrintComponent.prototype = new EventEmitter();
// Sets up listener for printing
PrintComponent.prototype.init = function () {
var self = this;
this.setPageOrientation(LANDSCAPE);
if (!window.matchMedia) {
return false;
}
window.matchMedia('print').addListener(function (e) {
self.onPrint(e);
});
};
// Handles printing event
PrintComponent.prototype.onPrint = function (e) {
var slideHeight;
if (!e.matches) {
return;
}
this.emit('print', {
isPortrait: this._orientation === 'portrait'
, pageHeight: this._pageHeight
, pageWidth: this._pageWidth
});
};
PrintComponent.prototype.setPageOrientation = function (orientation) {
if (orientation === PORTRAIT) {
// Flip dimensions for portrait orientation
this._pageHeight = PAGE_WIDTH;
this._pageWidth = PAGE_HEIGHT;
}
else if (orientation === LANDSCAPE) {
this._pageHeight = PAGE_HEIGHT;
this._pageWidth = PAGE_WIDTH;
}
else {
throw new Error('Unknown print orientation: ' + orientation);
}
this._orientation = orientation;
styler.setPageSize(this._pageWidth + 'px ' + this._pageHeight + 'px');
};
// Export singleton instance
module.exports = new PrintComponent();
},{"events":1,"components/styler":"syTcZF"}],"components/slide-number":[function(require,module,exports){
module.exports=require('GSZq7a');
},{}],"GSZq7a":[function(require,module,exports){
module.exports = SlideNumberViewModel;
function SlideNumberViewModel (slide, slideshow) {
var self = this;
self.slide = slide;
self.slideshow = slideshow;
self.element = document.createElement('div');
self.element.className = 'remark-slide-number';
self.element.innerHTML = formatSlideNumber(self.slide, self.slideshow);
}
function formatSlideNumber (slide, slideshow) {
var format = slideshow.getSlideNumberFormat()
, slides = slideshow.getSlides()
, current = getSlideNo(slide, slideshow)
, total = getSlideNo(slides[slides.length - 1], slideshow)
;
if (typeof format === 'function') {
return format.call(slideshow, current, total);
}
return format
.replace('%current%', current)
.replace('%total%', total);
}
function getSlideNo (slide, slideshow) {
var slides = slideshow.getSlides(), i, slideNo = 0;
for (i = 0; i <= slide.getSlideIndex() && i < slides.length; ++i) {
if (slides[i].properties.count !== 'false') {
slideNo += 1;
}
}
return Math.max(1, slideNo);
}
},{}],2:[function(require,module,exports){
// shim for using process in browser
var process = module.exports = {};
process.nextTick = (function () {
var canSetImmediate = typeof window !== 'undefined'
&& window.setImmediate;
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;
if (canSetImmediate) {
return function (f) { return window.setImmediate(f) };
}
if (canPost) {
var queue = [];
window.addEventListener('message', function (ev) {
var source = ev.source;
if ((source === window || source === null) && ev.data === 'process-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
return function nextTick(fn) {
queue.push(fn);
window.postMessage('process-tick', '*');
};
}
return function nextTick(fn) {
setTimeout(fn, 0);
};
})();
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.binding = function (name) {
throw new Error('process.binding is not supported');
}
// TODO(shtylman)
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
},{}],1:[function(require,module,exports){
(function(process){if (!process.EventEmitter) process.EventEmitter = function () {};
var EventEmitter = exports.EventEmitter = process.EventEmitter;
var isArray = typeof Array.isArray === 'function'
? Array.isArray
: function (xs) {
return Object.prototype.toString.call(xs) === '[object Array]'
}
;
function indexOf (xs, x) {
if (xs.indexOf) return xs.indexOf(x);
for (var i = 0; i < xs.length; i++) {
if (x === xs[i]) return i;
}
return -1;
}
// By default EventEmitters will print a warning if more than
// 10 listeners are added to it. This is a useful default which
// helps finding memory leaks.
//
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
var defaultMaxListeners = 10;
EventEmitter.prototype.setMaxListeners = function(n) {
if (!this._events) this._events = {};
this._events.maxListeners = n;
};
EventEmitter.prototype.emit = function(type) {
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events || !this._events.error ||
(isArray(this._events.error) && !this._events.error.length))
{
if (arguments[1] instanceof Error) {
throw arguments[1]; // Unhandled 'error' event
} else {
throw new Error("Uncaught, unspecified 'error' event.");
}
return false;
}
}
if (!this._events) return false;
var handler = this._events[type];
if (!handler) return false;
if (typeof handler == 'function') {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
var args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
return true;
} else if (isArray(handler)) {
var args = Array.prototype.slice.call(arguments, 1);
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
return true;
} else {
return false;
}
};
// EventEmitter is defined in src/node_events.cc
// EventEmitter.prototype.emit() is also defined there.
EventEmitter.prototype.addListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('addListener only takes instances of Function');
}
if (!this._events) this._events = {};
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit('newListener', type, listener);
if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
} else if (isArray(this._events[type])) {
// Check for listener leak
if (!this._events[type].warned) {
var m;
if (this._events.maxListeners !== undefined) {
m = this._events.maxListeners;
} else {
m = defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
console.trace();
}
}
// If we've already got an array, just append.
this._events[type].push(listener);
} else {
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
}
return this;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
var self = this;
self.on(type, function g() {
self.removeListener(type, g);
listener.apply(this, arguments);
});
return this;
};
EventEmitter.prototype.removeListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function');
}
// does not use listeners(), so no side effect of creating _events[type]
if (!this._events || !this._events[type]) return this;
var list = this._events[type];
if (isArray(list)) {
var i = indexOf(list, listener);
if (i < 0) return this;
list.splice(i, 1);
if (list.length == 0)
delete this._events[type];
} else if (this._events[type] === listener) {
delete this._events[type];
}
return this;
};
EventEmitter.prototype.removeAllListeners = function(type) {
if (arguments.length === 0) {
this._events = {};
return this;
}
// does not use listeners(), so no side effect of creating _events[type]
if (type && this._events && this._events[type]) this._events[type] = null;
return this;
};
EventEmitter.prototype.listeners = function(type) {
if (!this._events) this._events = {};
if (!this._events[type]) this._events[type] = [];
if (!isArray(this._events[type])) {
this._events[type] = [this._events[type]];
}
return this._events[type];
};
})(require("__browserify_process"))
},{"__browserify_process":2}],3:[function(require,module,exports){
var Api = require('./remark/api')
, polyfills = require('./polyfills')
, styler = require('components/styler')
;
// Expose API as `remark`