Skip to content

Instantly share code, notes, and snippets.

View trevnorris's full-sized avatar

Trevor Norris trevnorris

View GitHub Profile
Below results use latest iojs v1.x branch and node v0.11 branch.
$ /usr/bin/time iojs primality-test.js
13.01user 0.16system 0:13.16elapsed 100%CPU (0avgtext+0avgdata 682960maxresident)k
0inputs+0outputs (0major+45067minor)pagefaults 0swaps
$ /usr/bin/time node primality-test.js
3.05user 0.20system 0:03.25elapsed 100%CPU (0avgtext+0avgdata 871784maxresident)k
@trevnorris
trevnorris / tracing-additions.cc
Created February 5, 2015 18:25
How to hook into additional V8 tracing API. Also, commented out is how to use backtrace(3)
#include <v8.h>
#include <node.h>
#include <assert.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
native-stream.js uses the reference implementation, compiled with babel to run
on latest io.js.
stream-bluebird.js is exactly the same as native-stream.js except it uses the
latest bluebird Promise implementation.
node-stream.js uses the node stream implementation and forced to be async by
using process.nextTick() (which is functionally equivalent to the microtask
processor)

This is a very rough and verbose overview of an API that is simplistic and can be kept consistent across the entire code base. I visited many API possibilities that could have been used, and this API came out as the best combination of everything available.

First we'll start with examples from the file system API. This is a partial set of all features that already exist, also included are some new APIs.

File System

@trevnorris
trevnorris / generator-vs-stateful.js
Last active August 29, 2015 14:16
Comparing generators with stateful objects, for performance
'use strict';
const ITER = 1e7;
let t;
function* G() {
let i = 0;
for (; i < ITER; i++)
yield i;
}
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000000001fb50f0 in ?? ()
(gdb) bt
#0 0x0000000001fb50f0 in ?? ()
#1 0x000000000040be54 in uv__finish_close (handle=0x1df91e0) at lib/libnub/deps/uv/src/unix/core.c:245
#2 uv__run_closing_handles (loop=0x1b141e0) at lib/libnub/deps/uv/src/unix/core.c:259
#3 uv_run (loop=0x1b141e0, mode=UV_RUN_DEFAULT) at lib/libnub/deps/uv/src/unix/core.c:326
#4 0x0000000000405eef in hw_http_open ()
#5 0x0000000000403b57 in main ()
(gdb) f 1
@trevnorris
trevnorris / promise-logic-reasons.txt
Last active March 27, 2016 14:32
Reasons why it's not good to use Promises for logical flow control
From Bradley Meck (https://twitter.com/bradleymeck)
1. when using promises for notification you need to avoid recursively calling
.then , it is easy to make nested closures if a fn in .then produces another fn
2. you are mixing logic, .then vs .catch as a means to branch is not quite the
same as try/catch (and they don't have a finally mechanism)
3. you can use logic inside of a .then handler to route creation of a new
promise which is fine, but if that promise is returned you get into major

In the following example data is being read from a file, undergoes a transform then is written to a port. How are the following things handled?

  1. If attempting to write to the port results in an error, how do we let the file know it's time to close?

  2. If the file is deleted under our feet, how do we let the port know to close?

  3. If the transform has a parsing error how does it alert both the file and the port they need to close and cleanup?

@trevnorris
trevnorris / .eslintrc
Last active August 29, 2015 14:18
My .eslintrc of choice. Still have a few things to work out.
{
'ecmaFeatures': {
'binaryLiterals': true,
'blockBindings': true,
'forOf': true,
'generators': true,
'octalLiterals': true,
'templateStrings': true,
},
'env': {
'use strict';
class Readable {
constructor() {
this._onreadable = null;
// Acquire the default onreadable callback if it's been set.
if (typeof this.constructor._onreadable === 'function')
this._onreadable = this.constructor._onreadable;
}