2. About Me
Work for the Mozilla Corporation (Firefox!)
ā¦
ā¦ Do a lot of JavaScript performance analysis
ā¦ Dromaeo.com is my performance test suite
ā¦ Tests the performance of JavaScript engines
ā¦ Tests the performance of browser DOM
Creator of the jQuery JavaScript Library
ā¦
ā¦ http://jquery.com/
ā¦ Used by Microsoft, Google, Adobe, Nokia,
IBM, Intel, CBS News, NBC, etc.
3. Upcoming Browsers
The browsers:
ā¦
ā¦ Firefox 3.1
ā¦ Safari 4
ā¦ Internet Explorer 8
ā¦ Opera 10
ā¦ Google Chrome
Deļ¬ning characteristics:
ā¦
ā¦ Better performance
ā¦ Advanced JavaScript engines
4. Firefox 3.1
Set to be released Spring 2009
ā¦
Goals:
ā¦
ā¦ Performance improvements
ā¦ Video and Audio tags
ā¦ Private browsing
Beta 2 just released
ā¦
http://developer.mozilla.org/En/Firefox_3.1_for_developers
ā¦
5. Safari 4
Released in conjunction with OS X 10.6
ā¦
Features:
ā¦
ā¦ Performance improvements
ā¦ Desktop Apps
ā¦ ACID 3 compliance
ā¦ Revamped dev tools
Preview seeded to developers
ā¦
http://webkit.org/blog/
ā¦
6. Internet Explorer 8
Released early 2009
ā¦
Features:
ā¦
ā¦ Backwards compatibility with IE 7
ā¦ Web Clips (trim out a part of a page and
place on desktop)
ā¦ Process per tab
RC1 recently
ā¦
released
http://www.microsoft.com/
ā¦
windows/internet-explorer/
beta/readiness/new-features.aspx
7. Opera 10
Unspeciļ¬ed release schedule (2009?)
ā¦
Features:
ā¦
ā¦ ACID 3 compliance
ā¦ Video and Audio tags
Opera 9.6 recently released
ā¦
http://labs.opera.com/
ā¦
8. Google Chrome
Chrome 1.0 September 2008
ā¦
Features:
ā¦
ā¦ Private browsing
ā¦ Process per tab
Chrome 2.0 upcoming
ā¦
http://googlechromereleases.blogspot.com/
ā¦
9. Process Per Tab
Most browsers have a single process
ā¦
ā¦ Share memory, resources
ā¦ Pages rendered using threads
IE 8 and Chrome split tabs into individual
ā¦
processes
What does this change?
ā¦
ā¦ Pages can do more processing
ā¦ (Not block the UI or other tabs)
ā¦ Tabs consume more memory
10. Process Per Tab
Examples of changes, in Chrome.
ā¦
Timer speed is what you set it to.
ā¦
ā¦ Browsers cap the speed to 10ms.
ā¦ setInterval(fn, 1);
Can do more non-stop processing, without
ā¦
warning:
while (true) {}
Chrome has a process manager (like the
ā¦
OS process manager - see CPU, Memory)
11. JavaScript Engines
New wave of engines:
ā¦
ā¦ Firefox: TraceMonkey
ā¦ Safari: SquirrelFish (Extreme)
ā¦ Chrome: V8
Continually leap-frogging each other
ā¦
12. Common Areas
Virtual Machines
ā¦
ā¦ Optimized to run a JavaScript-speciļ¬c
bytecode
Shaping
ā¦
ā¦ Determine if two objects are similar
ā¦ Optimize behavior based upon that
ā¦ āWalks like a duck, quacks like a duckā
ā¦ if ( obj.walks && obj.quacks ) {}
14. Bytecode
Speciļ¬c low-level commands
ā¦
Written in machine code
ā¦
a + b;
ā¦
PLUS( a, b ) {
ā¦
IF ISSTRING(a) OR ISSTRING(b) {
return CONCAT( a, b );
} ELSE {
return ADD( a, b );
}
}
17. TraceMonkey
Firefox uses an engine called
ā¦
SpiderMonkey (written in C)
Tracing technology layered on for Firefox
ā¦
3.1 (dubbed āTraceMonkeyā)
Hyper-optimizes repeating patterns into
ā¦
bytecode
http://ejohn.org/blog/tracemonkey/
ā¦
18. Tracing
for ( var i = 0; i < 1000; i++ ) {
ā¦
var foo = stuļ¬[i][0];
foo = āmore stuļ¬ ā + someFn( foo );
}
function someFn( foo ) {
return foo.substr(1);
}
Loop is costly
ā¦
ā¦ ISNUM(i)
ā¦ ADD(i, 1)
ā¦ COMPARE( i, 1000 )
19. Function Inlining
for ( var i = 0; i < 1000; i++ ) {
ā¦
var foo = stuļ¬[i][0];
foo = āmore stuļ¬ ā + foo.substr(1);
}
20. SquirrelFish
Just-in-time compilation for JavaScript
ā¦
Compiles a bytecode for common
ā¦
functionality
Specialties:
ā¦
ā¦ Bytecodes for regular expressions (super-
fast)
http://arstechnica.com/journals/linux.ars/2008/10/07/extreme-
ā¦
javascript-performance
21. Chrome V8
Makes extensive use of shaping (fast
ā¦
property lookups)
Hyper-optimized function calls and
ā¦
recursion
Dynamic machine code generation
ā¦
http://code.google.com/p/v8/
ā¦
22. Measuring Speed
SunSpider
ā¦
ā¦ Released by the WebKit team last fall
ā¦ Focuses completely on JavaScript
Dromaeo
ā¦
ā¦ Released by Mozilla this spring
ā¦ Mix of JavaScript and DOM
V8 Benchmark
ā¦
ā¦ Released by Chrome team last month
ā¦ Lots of recursion testing
Quality: http://ejohn.org/blog/javascript-benchmark-quality/
ā¦
27. Network
Steve Soudersā UA tool:
ā¦
http://stevesouders.com/ua/
Also see: YSlow
ā¦
28. Simultaneous Conn.
Number of downloads per domain
ā¦
Should be at least 4
ā¦
ā¦ FF 2, IE 6, and IE 7 are 2
ā¦ Safari is 4
ā¦ Everyone else is 6-7
Max connections: Number of simultaneous
ā¦
downloads
ā¦ Firefox, Opera: 25-30
ā¦ Everyone else: 50-60
29. Parallel Scripts
Download scripts simultaneously
ā¦
Even though they must execute in order
ā¦
<script defer>
ā¦
ā¦ From Internet Explorer
ā¦ Just landed for Firefox 3.1
ā¦ In Opera as well
ā¦ Replacement for JavaScript-based
loading
30. Redirect Caching
A simple request:
ā¦
http://foo.com ->
http://www.foo.com ->
http://www.foo.com/
Very costly, should be cached.
ā¦
Chrome and Firefox do well here.
ā¦
31. Link Prefetching
<link rel=āprefetchā href=āsomeimg.pngā/>
ā¦
Pre-load resources for later use
ā¦
ā¦ (images, stylesheets)
Currently only in Firefox
ā¦
Replacement for JavaScript preloading
ā¦
33. postMessage
A way to pass messages amongst multiple
ā¦
frames and windows
Implemented in all browsers (in some
ā¦
capacity).
Sending a message:
ā¦
iframe.postMessage(ātestā,
ā¦
āhttp://google.com/ā);
34. postMessage
Receiving a Message:
ā¦
window.addEventListener(āmessageā, function(e){
ā¦
if (e.origin !== āhttp://example.com:8080ā)
return;
alert( e.data );
}, false);
35. Cross-Domain XHR
Landed in Firefox 3.1, support in IE 8
ā¦
Add a header to your content:
ā¦
Access-Control-Allow-Origin: *
XMLHttpRequest can now pull it in, even
ā¦
across domains
https://developer.mozilla.org/En/
ā¦
HTTP_Access_Control
37. Class Name
New method:
ā¦
getElementsByClassName
Works just like:
ā¦
getElementsByTagName
Fast way of ļ¬nding elements by their class
ā¦
name(s):
<div class=āperson sidebarā></div>
document.getElementsByClassName(āsidebarā)
ā¦
Safari 3.1, Firefox 3.0, Opera 9.6
ā¦
Drop-in replacement for existing queries
ā¦
39. Selectors API
querySelectorAll
ā¦
Use CSS selectors to ļ¬nd DOM elements
ā¦
document.querySelectorAll(ādiv pā)
ā¦
Good cross-browser support
ā¦
ā¦ IE 8, Safari 4, FF 3, Opera 10
Drop-in replacement for JavaScript
ā¦
libraries.
43. HTML 5 Dragging
Includes full support drag and drop events
ā¦
Events: dragstart, dragend, dragenter,
ā¦
dragleave, dragover, drag, drop
<div draggable=ātrueā
ā¦
ondragstart=āevent.dataTransfer.setData
(ātext/plainā, āThis text may be draggedā)ā>
This text <strong>may</strong> be
dragged.
</div>
Only in Firefox 3.1
ā¦
44. Bounding
getBoundingClientRect
ā¦
ā¦ Introduced by IE
ā¦ Seeing a wider introduction
Super-fast way to determine the position
ā¦
of an element
Better alternative to manual computation
ā¦
45. JavaScript Threads
JavaScript has always been single-threaded
ā¦
Limited to working linearly
ā¦
New HTML 5 Web Workers
ā¦
Spawn JavaScript threads
ā¦
Run complicated work in the background
ā¦
ā¦ Doesnāt block the browser!
Drop-in usable, huge quality boost.
ā¦
46. A Simple Worker
var myWorker = new Worker(āmy_worker.jsā);Ā Ā
ā¦
myWorker.onmessage = function(event)Ā {Ā Ā
alert(āCalled back by the worker!nā);Ā Ā
};Ā Ā
47. Styling and Eļ¬ects
Lots of commons styling eļ¬ects
ā¦
Can be replaced and simpliļ¬ed by the
ā¦
browser
Drastically simplify pages and improve
ā¦
their performance
48. Rounded Corners
CSS 3 speciļ¬cation
ā¦
Implemented in Safari, Firefox, Opera
ā¦
ā¦ -moz-border-radius: 5px;
ā¦ -webkit-border-radius: 5px;
Can replace clumsy, slow, old methods.
ā¦
49. Shadows
Box Shadows
ā¦
ā¦ Shadow behind a div
-webkit-box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.5)
ā¦
Text Shadows
ā¦
ā¦ Shadow behind some text
text-shadow: -1px -1px #666, 1px 1px #FFF;
ā¦
Implemented in WebKit, Firefox
ā¦
Can replace clumsy, slow, old methods.
ā¦
50. Example Shadows
Demos: http://maettig.com/code/css/text-
ā¦
shadow.html
http://webkit.org/blog/86/box-shadow/
51. Custom Fonts
Load in custom fonts
ā¦
Can load TrueType fonts
ā¦
Implemented in Safari and Firefox
ā¦
Demo: http://ejohn.org/apps/fontface/
ā¦
blok.html
Can replace using Flash-based fonts.
ā¦
52. Transforms and Animations
Transforms allow you to rotate, scale, and
ā¦
oļ¬set an element
ā¦ -webkit-transform: rotate(30deg);
Animations allow you to morph an
ā¦
element using nothing but CSS
ā¦ -webkit-transition: all 1s ease-in-out;
Implemented in WebKit and Firefox
ā¦
Demo: http://www.the-art-of-web.com/css/
ā¦
css-animation/
Can replace JS animations, today.
ā¦
53. Canvas
Proposed and ļ¬rst implemented by Apple
ā¦
in WebKit
A 2d drawing layer
ā¦
ā¦ With possibilities for future expansion
Embedded in web pages (looks and
ā¦
behaves like an img element)
Works in all browsers (IE with ExCanvas)
ā¦
Oļ¬oad rendering to client
ā¦
Better user interaction
ā¦
54. Shapes and Paths
NOT vectors (unlike SVG)
ā¦
Rectangles
ā¦
Arcs
ā¦
Lines
ā¦
Curves
ā¦
Charts:
ā¦
55. Fill and Stroke
All can be styled (similar to CSS)
ā¦
Stroke styles the path (or outline)
ā¦
Fill is the color of the interior
ā¦
Sparklines:
ā¦
56. Canvas Embedding
Canvases can consume:
ā¦
ā¦ Images
ā¦ Other Canvases
Will be able to consume (Firefox 3.1, Safari
ā¦
4):
ā¦ Video
In an extension:
ā¦
ā¦ Web Pages
58. SQL Storage
Part of HTML 5 - a full client-side SQL
ā¦
database (SQLite)
Implemented in WebKit
ā¦
var database = openDatabase(ādbā, ā1.0ā);
ā¦
database.executeSql(āSELECT * FROM testā, function(result1) {
// do something with the results
database.executeSql(āDROP TABLE testā);
});
59. Native JSON
JSON is a format for transferring data
ā¦
ā¦ (Uses JavaScript syntax to achieve this.)
ā¦ Has been slow and a little hacky.
Browser now have native support in
ā¦
Firefox 3.1 and IE 8
Drop-in usable, today
ā¦
ā¦ JSON.encode(object)
ā¦ JSON.decode(string)
62. Painting
When something is physically drawn to
ā¦
the screen
Hard to quantify without more
ā¦
information
Firefox 3.1 includes a new event:
ā¦
MozAfterPaint
Demo: http://ejohn.org/blog/browser-
ā¦
paint-events/
64. Reļ¬ow
CSS has lots of boxes in it
ā¦
Position of boxes is constantly recomputed
ā¦
and repositioned (before being painted)
ā¦ This is reļ¬ow
Demo: http://dougt.wordpress.com/
ā¦
2008/05/24/what-is-a-reļ¬ow/
65. Questions?
John Resig
ā¦
http://ejohn.org/
http://twitter.com/jeresig/