Skip to content

Instantly share code, notes, and snippets.

@bjouhier
bjouhier / doesnotexit.js
Created May 6, 2012 17:03
luvmonkey program that does not exit
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 / primes.js
Created April 28, 2012 14:30
Primes generator
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 / benchCallbacks.js
Created April 11, 2012 20:01
streamline vs. callbacks bench
"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 / needsTrampoline.js
Created March 9, 2012 21:12
Simple code that breaks without trampoline
if (!require('streamline/module')(module)) return;
function fibo(_, n) {
return n > 1 ? fibo(_, n - 1) + fibo(_, n - 2) : 1;
}
function fibo2(_, n) {
return n > 1 ? trampo(this, 0, fibo2, _, n - 1) + trampo(this, 0, fibo2, _, n - 2) : 1;
}
@bjouhier
bjouhier / bench.js
Created February 13, 2012 08:16
Simple bench of basic JS constructs
function bench(name, fn) {
var result;
// 2 passes to let V8 optimize
for (var pass = 2; pass >= 0; pass--) {
var count = 1;
while (true) {
var t0 = Date.now();
fn(count);
dt = (Date.now() - t0);
if (dt > 100) {
@bjouhier
bjouhier / chomp.js
Created February 2, 2012 10:45
Streamlined function to concatenate several input files into one output file
"use strict";
if (!require('streamline/module')(module)) return;
var fs = require('fs');
var streams = require('streamline/lib/streams/server/streams');
// cat ins > out
exports.chomp = function(ins, out, _) {
streams.usingWritable(_, fs.createWriteStream(out), function(_, outStream) {
for (var i = 0; i < ins.length; i++) {
@bjouhier
bjouhier / gist:1535403
Created December 29, 2011 18:19
my first ace test
#!/usr/bin/env node-streamline
"use strict";
require('coffee-script')
var test = require("ace/src/lib/ace");
test(1, function(_) {
function Foo(x, _) {
process.nextTick(_);
this.x = x;
@bjouhier
bjouhier / tryCatchTrampo.js
Created December 18, 2011 12:09
Callback wrapper that implements trampoline with try/catch
// callback wrapper for trampoline
var depth = 0;
var maxDepth = 100;
var marker = {};
function trampo(cb) {
return function(e, r) {
var d = depth++;
try {
if (depth === maxDepth)
@bjouhier
bjouhier / callrepro
Created December 17, 2011 14:20
fstreamline bug repro with fn.call
"use strict";
if (!require('streamline/module')(module)) return;
function foo(_, i) {
return 2 * i;
}
function bar(_, f, i) {
return f.call(null, _, i);
}
@bjouhier
bjouhier / jsurlbench.js
Created November 6, 2011 13:27
jsurl bench
"use strict";
var JSURL = require("jsurl");
function bench(name, count, fn) {
var t0 = Date.now();
for (var i = 0; i < count; i++)
fn();
console.log(name + ": " + (Date.now() - t0));
}