Skip to content

Instantly share code, notes, and snippets.

View billywhizz's full-sized avatar
🤓
always be learning

Andrew Johnston billywhizz

🤓
always be learning
View GitHub Profile
@billywhizz
billywhizz / bencCallbacks.js
Created April 12, 2012 12:40
response to bruno's streamline benchmark
"use strict";
var fs = require('fs');
var cache = {}, hit = 0, missed = 0;
var count = parseInt(process.argv[2]) || 1000000;
var fname = __dirname + "/benchCallbacks.js";
function bench(cb) {
var total = 0;
var current = 0;
function handleResult(err, data) {
@billywhizz
billywhizz / results.txt
Created April 10, 2012 17:02
streamline.js overhead
test.js:
29,222,676 calls per second
test-named:
50,505,050 calls per second
test-streamline.js:
332,557 calls per second
@billywhizz
billywhizz / http.js
Created March 29, 2012 03:49
node.js url escaping example
var http = require("http");
http.createServer(function (req, res) {
console.log(require("url").parse(req.url, true));
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(80, '0.0.0.0');
@billywhizz
billywhizz / haiku.js
Created March 1, 2012 11:16
haiku test
res.writeHead(200);
throw(new Error("poof!"));
res.end();
@billywhizz
billywhizz / bench.js
Created February 14, 2012 23:50
node.js static file serving benchmark
var TCP = process.binding("tcp_wrap").TCP;
var Buffer = process.binding("buffer").SlowBuffer;
var HTTPParser = process.binding("http_parser").HTTPParser;
function setupSocket(peer) {
function shutdownHandler(status, handle, req) {
// TODO: ensure we only shutdown once
if(status != 0) {
if(peer.onerror) {
var err = new Error("shutdown");
@billywhizz
billywhizz / nodeftpd.js
Created January 13, 2012 20:03
ftp api
var FTPServer = require("nodeftpd").Server;
var options = {
host: "localhost",
pasvip: "127.0.0.1",
port: 21,
portrange: {
lo: 1025,
hi: 2048
},
@billywhizz
billywhizz / handletest.js
Created January 12, 2012 17:30
server listen on existing libuv handle
var net = require("net");
var TCP = process.binding("tcp_wrap").TCP;
var Pipe = process.binding("pipe_wrap").Pipe;
var sock1 = new TCP();
sock1.bind("0.0.0.0", 80);
var sock2 = new Pipe();
sock2.bind("/tmp/handle.sock");
var server1 = net.createServer(function(c) {
@billywhizz
billywhizz / tcp.js
Created December 6, 2011 18:53
minimal node.js tcp server
var TCP = process.binding("tcp_wrap").TCP;
try {
var crypto = process.binding("crypto");
var SecureContext = crypto.SecureContext;
} catch (e) {
throw new Error("node.js not compiled with openssl crypto support.");
}
function noop() {};
function createCredentials(key, cert, ciphers) {
@billywhizz
billywhizz / bufftest.js
Created December 3, 2011 18:25
testing buffer write performance
Buffer.prototype.xwriteUInt8 = function(value, offset) {
this[offset] = value & 0xff;
}
Buffer.prototype.xwriteUInt16BE = function(value, offset) {
this[offset++] = (value >>> 8) & 0xff;
this[offset] = value & 0xff;
}
Buffer.prototype.xwriteUInt32BE = function(value, offset) {
@billywhizz
billywhizz / fib.js
Created December 1, 2011 14:51
cluster example
var cluster = require('cluster');
function fib (n) {
if (n < 2) {
return 1;
}
else {
return fib(n-2) + fib(n-1);
}
}