Skip to content
Snippets Groups Projects
Commit f6e7a44a authored by Stefan Dierauf's avatar Stefan Dierauf
Browse files

did more slides

parent be9c4bfd
No related branches found
No related tags found
No related merge requests found
Showing
with 117 additions and 73 deletions
pics/github_new.png

176 KiB

## Anonymous Callbacks
Don't need to explicitly name a function, instead just write the function as a parameter:
```javascript
// Anonymous inlined callback
helloCaller("Bob Bobberson", function(name) {
// Finnish Hello
console.log("Moi " + name);
});
```
<pre>
Moi Bob Bobberson</pre>
You see this **all the *time*** when dealing with nodejs and javascript in general
\ No newline at end of file
## Event Loop
* What if
\ No newline at end of file
* Async calls enqueued on event loop
* Node single-thread will dequeue and process
* When there's nothing left to do, node will sleep
## Google v8
* Google's open source javascript engine
* Used by google chrome
* Compiles javascript to machine code JIT
*
'''''''''''show how node connects with c++ code?
'''''''''''show assembly? benchmarks?
## How is node put together?
Three main components:
* Google V8 engine that compiles/optimizes Javascript
* High performance http parser
* Google v8 engine that compiles/optimizes Javascript
* High performance parts written in C (http, etc)
* Bridge between high performance parts to v8 (via v8 apis)
* Libuv as an abstraction layer for network and file system
## Javascript example #1
Some simple DOM manipulation
```javascript
// Grab every div on the page
var divs = document.querySelectorAll("div");
// Loop through each element
for (var i = 0; i < divs.length; i++) {
// Change the background image
divs[i].style.backgroundImage =
"url('http://stfn.me/stefchat/stefchatweb.jpg')";
}
```
\ No newline at end of file
## Javascript Example #2
First class functions!!
```javascript
function englishHello(name) {
console.log("Hello " + name);
}
function swedishHello(name) {
console.log("Hej " + name);
}
var cse333 = swedishHello;
function helloCaller(name, helloFunction) {
helloFunction(name);
}
helloCaller("Brendan Eich", englishHello);
helloCaller("Grace Hopper", swedishHello);
helloCaller("Abraham Lincoln", cse333);
```
<pre>
Hello Brendan Eich
Hej Grace Hopper
Hej Abraham Lincoln</pre>
## Javascript to the rescue
* Designed in 10 days by Brendan Eich
* @see <a href="https://www.destroyallsoftware.com/talks/wat">Wat by Gary Bernhardt</a>
* Dynamic typing
* First class functions
* **Event loop**
* **Non-blocking I/O**
......@@ -2,8 +2,19 @@
"nodejs.md",
"who-is-this-kid.md",
"outline.md",
"what-is-node.md",
"the-problem.md",
"the-solution.md",
"why-node-is-awesome.md",
"why-node-is-awesome-pt-2.md",
"who-uses-node.md",
"how-is-node-put-together.md"
"how-is-node-put-together.md",
"google-v8.md",
"syscall-overhead.md",
"v8-benchmarks.md",
"pure-c-modules.md",
"outline-pt2.md",
"the-javascript-part.md",
"what-is-a-callback.md",
"event-loop.md"
]
\ No newline at end of file
## Outline
1. What is node/Why node is awesome
2. Nodejs architecture overview
3. V8, c++, and optimizing oh my
4. **Why javascript? (aka all glory to the event loop)**
5. Recap + takeaways
## Outline
1. Why node is awesome
1. What is node/Why node is awesome
2. Nodejs architecture overview
3. V8, c++, and optimizing oh my
4. Why javascript? (aka all glory to the event loop)
......
## Pure C modules
Most of node's components are written in javascript,
but a few high performance ones are written in pure C
and then connect into v8 via v8 APIs
* https://github.com/joyent/node/tree/master/deps/http_parser
* networking?
'''''''''''check with xi
## Syscall overhead
Hello world in C, node, and Java
| Language | Syscall Count |
|----------|---------------|
| C | 13 |
| Java | 317 |
| Node | 491 |
## The javascript part
* Javascript is one of the most popular languages in the world
<img src="pics/github_new.png" style="width: 400px">
* Lots of people with knowledge of javascript
* Ergo, lots of people familiar with async + callbacks
* Nodejs uses lots of async + callbacks
\ No newline at end of file
## The Problem
* Servers have to handle lots of requests
* majority of I/O is disk bound
```
var result = database.query("kittens");
// twaddle fingers
send(result);
```
Apache makes a new thread per request,
<br>
but uses hella memory
\ No newline at end of file
## The Solution
Nodejs uses callbacks to defer dealing with I/O until it's ready
```
database.query("kittens", function (data) {
// database is done and has the data ready
send(data);
});
// do other servery things
```
## v8 benchmarks
'''''''''''''grab some benchmarks online
## What even *is* node?
## what is a callback
Callbacks are functions that are passed as parameters to other functions,
and then called within those functions
LinkedList implementation requires a function pointer to free a payload,
same idea
```
function helloCaller(helloFn, name) {
helloFn(name);
}
function hello(name) { console.log("hello " + name); }
function swedishHello(name) { console.log("hej " + name); }
helloCaller(hello, "CSE 333");
helloCaller(swedishHello, "CSE 333");
```
\ No newline at end of file
## What is node
* Asynchronous, event driven server framework
* Non-blocking I/O
* For frontend js noobs and "unix haxors"
* Single threaded!
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment