Skip to content

Instantly share code, notes, and snippets.

@xk
xk / bench.js
Created April 11, 2012 21:00
When threads_a_gogo beats node 2 to 1
/*
$ node bench.js
Threads_a_gogo JS thread -> 4624 (ms) 298607040
Node's main JS thread -> 8471 (ms) 298607040
Ratio: 1.83 times faster
*/
function fib (n) {
@xk
xk / fib.js
Created March 13, 2012 20:27 — forked from ry/fib.js
a proper fibonacci server in node. with threads_a_gogo
var http = require('http')
var pool = require('threads_a_gogo').createPool(5).all.eval(fib);
function fib (n) {
if (n < 2) {
return 1;
} else {
return fib(n - 2) + fib(n - 1);
}
}
@xk
xk / gist:2001931
Created March 8, 2012 16:29
Install threads-a-gogo
jorge@UbuntuParallels:~$ git clone http://github.com/xk/node-threads-a-gogo.git
Cloning into node-threads-a-gogo...
remote: Counting objects: 112, done.
remote: Compressing objects: 100% (90/90), done.
remote: Total 112 (delta 16), reused 112 (delta 16)
Receiving objects: 100% (112/112), 59.80 KiB | 84 KiB/s, done.
Resolving deltas: 100% (16/16), done.
jorge@UbuntuParallels:~$ cd node-threads-a-gogo/
jorge@UbuntuParallels:~/node-threads-a-gogo$ node-waf configure install
@xk
xk / nextTick.js
Created December 22, 2011 21:39
nextTick.js
/*
2012-04-22 jorge@jorgechamorro.com
MacBookPro5,4 core2duo @ 2.53GHz, node v0.7.5:
loopsPerSecond: 103412616.3, nextTicksPerSecond: 671885.8, ratio: 153.9x times faster
beagleboard beaglebone @ 500Mhz:
loopsPerSecond: 6781040.2, nextTicksPerSecond: 27472.4, ratio: 246.8x times faster
*/
@xk
xk / SubArray.js
Created October 28, 2011 06:59 — forked from CrabDude/SubArray.js
Node.js Array subclass
// 20111028 jorge@jorgechamorro.com
// How to subclass Array
// In reply to https://gist.github.com/1100841/9c959db9314338a09c0f288c2c0ca5553816e400
function subArray () {
var nu= Array.apply(null, arguments);
nu.__proto__= subArray.prototype;
return nu;
}
@xk
xk / timers.js
Created September 14, 2011 09:13
test node's timers
// 2011-09-15 jorge@jorgechamorro.com
// http://groups.google.com/group/nodejs-dev/msg/cf392e131b5fcd3a
// http://groups.google.com/group/nodejs-dev/browse_thread/thread/922a30cf88a1b784
var ms= 5;
var ctr= 0;
var RUN= 1;
function f () {
if (RUN) {
@xk
xk / status.js
Created September 12, 2011 23:27
app status
var appStatus;
process.on('uncaughtException', function globalErrHandler (error) {
console.log('An error happened when the appStatus was '+ appStatus);
switch (appStatus) {
case 1: //recover from status 1
case 2: //idem from status 2
case 3: //etc.
@xk
xk / gist:991013
Created May 25, 2011 13:52 — forked from virtuo/gist:757317
Problem using crypto with base64 ouput format
var crypto = require('crypto');
var key = "some keys are better than others";
var msg = "Attack Troy from within a wooden horse!";
var CYPHER = "aes256";
var test = function(msg) {
var cypher = crypto.createCipher(CYPHER, key);
var enc_msg = cypher.update(msg, "utf8", 'binary');
enc_msg += cypher.final('binary');
@xk
xk / problem2.js
Created May 1, 2011 11:49
The problem has something to do with the GC.
/*
problem2.js, 20110501 jorge@jorgechamorro.com
to run: $node --expose-gc problem2.js
problem2.js is just like problem.js, but does a gc() whenever a BAD happens.
After the gc() it runs fine again for quite while, so the problem must have something to do with the GC.
******************
@xk
xk / setTimeoutProblem.js
Created April 20, 2011 21:00
Passing a float (instead of an integer) drives node's setTimeout nuts.
// sets kCount timeouts of no more than 5 ms using floats and using integers
// and logs the times at which they are actually triggered.
var t0;
var log= [];
var kCount= 20;
function go () {
var i= kCount;
var cb= f('FLOAT');