Skip to content

Instantly share code, notes, and snippets.

@bjouhier
bjouhier / benchCallbacks.js
Created Apr 11, 2012
streamline vs. callbacks bench
View benchCallbacks.js
"use strict";
var fs = require('fs');
var cache = {}, hit = 0, missed = 0;
function load(name, cb) {
var res = cache[name];
if (res) {
process.nextTick(function() {
hit++;
cb(null, res);
@bjouhier
bjouhier / primes.js
Created Apr 28, 2012
Primes generator
View primes.js
exports.generator = function() {
var found = [];
function isPrime(p) {
for (var i = 0; i < found.length; i++) {
var q = found[i];
if (p % q === 0) return false;
if (q * q > p) return true;
}
return true;
@bjouhier
bjouhier / doesnotexit.js
Created May 6, 2012
luvmonkey program that does not exit
View doesnotexit.js
var Timer = require('uv').Timer;
function foo() {
var timer = new Timer();
timer.start(1000, 0, function(err) {
f.send("ok");
f.close();
})
var r = yield;
print("got " + r);
@bjouhier
bjouhier / asyncgen.js
Created May 18, 2012
Asynchronous hello world with JS generators
View asyncgen.js
(function() {
var GENERATOR_PROTO = Object.getPrototypeOf((function() {
yield;
})());
function isGenerator(val) {
return typeof val === 'object' && Object.getPrototypeOf(val) === GENERATOR_PROTO;
}
var PENDING = {};
@bjouhier
bjouhier / benchNextTick.js
Created May 29, 2012
Benching setTimeout against process.nextTick
View benchNextTick.js
"use strict";
var count = 1000000;
function bench(fn, cb) {
var total = 0;
function loop(i) {
if (i === count) {
cb(null, total);
} else {
@bjouhier
bjouhier / hackGlobal.js
Created Jun 2, 2012
Wrapping a function to hack a global temporarily
View hackGlobal.js
var g = 3;
function hackGlobal(fn) {
return function() {
var saveG = g;
try {
g = 5;
fn.apply(this, arguments);
} finally {
g = saveG;
@bjouhier
bjouhier / demo1-original.js
Created Jul 4, 2012
equivalence between event-oriented and callback-oriented stream API
View demo1-original.js
"use strict";
var wrappers = require('./wrappers');
var http = require('http');
var zlib = require('zlib');
var util = require('util');
var fs = require('fs');
http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain; charset=utf8',
View wrappers._js
// Wrapping asynchronous functions
// This wrapper is implemented with callbacks
// i is the index of the callback in the parameter list
// isFuture is a reserved parameter - do not pass it
function wrapper1(fn, i, isFuture) {
return function wrapped() {
var cb = arguments[i];
if (!isFuture) console.log("WRAPPER1 BEFORE CALL: " + fn.name);
if (cb == null) return wrapper1(fn.apply(this, arguments), 0, true);
arguments[i] = function(err, result) {
@bjouhier
bjouhier / testgen._js
Created May 9, 2013
streamlined harmony generators example based on https://gist.github.com/creationix/5544019
View testgen._js
var fs = require('fs');
function testgen(_) {
console.log(fs.readFile(__filename, _));
console.log("Sleeping for 2000ms...");
setTimeout(_, 2000);
console.log("Done");
}
testgen(_);
@bjouhier
bjouhier / fiboBench.js
Created May 10, 2013
First bench to compare raw callbacks and the 3 streamline modes: callbacks, fibers and generators
View fiboBench.js
var fs = require('fs');
function fib(n) {
return n <= 1 ? 1 : fib(n - 1) + fib(n - 2);
}
function rawBench(n, loop, modulo, asyncFn, callback) {
var count = 0;
var l = loop;
You can’t perform that action at this time.