Skip to content

Instantly share code, notes, and snippets.

@xk
xk / selfShadowingGetter.js
Created March 8, 2011 23:46
selfShadowingGetter.js
//20110308 jorge@jorgechamorro.com
Object.defineProperty(thePrototype, 'parsedUrl', {
get: function () {
var parsed= url.parse(this.url, true);
this.__proto__= null; // cuts momentarily the proto chain
this.parsedUrl= parsed; // shadows the getter
this.__proto__= thePrototype; // restores the proto chain
return parsed;
}
// testing the effect of using an ES5 Getter
// to get the parsedUrl on an object
var url = require("url")
function AutoParseUrl (u) {
this.url = u
}
Object.defineProperty(AutoParseUrl.prototype, "parsedUrl", {get:function () {
return url.parse(this.url)
@xk
xk / test.js
Created April 6, 2011 07:00
test socket.write()
var a= 'a';
var kMaxLength= Math.pow(2,27);
while (a.length < kMaxLength) {
a= [a,a].join('');
console.log("*** BEGIN -> " + a.length);
console.log(a);
console.log("*** END -> " + a.length);
@xk
xk / test.js
Created April 6, 2011 08:17
test #2 for socket.write()
var a= 'a';
var kMaxLength= 8192;
do a= [a,a].join(''); while (a.length < kMaxLength);
var i= 1e4;
while (i--) console.log(a);
a= null;
@xk
xk / noproblem.js
Created April 7, 2011 22:08
from main.js it works fine... (?)
var str = '';
var n= 1024*1024;
var i = n;
while (i--) str += "C";
console.log('str.length -> ' + str.length);
var maxT= -1;
var save= [];
@xk
xk / problem.js
Created April 10, 2011 00:27
This is the problem of http-simple.js
/*
problem.js, 20110417 jorge@jorgechamorro.com
Demonstrates a problem in Buffer.prototype.write() with cons-strings (also known as ropes)
run this as node --trace-gc problem.js
and you'll see that too often the buf.write() takes ~ 50x times as long to complete (marked as '**** BAD').
After a GC cycle it returns to normal/good for a little while.
@xk
xk / problem
Created April 20, 2011 17:09
installing v8monkey
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-2.13.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 433k 100 433k 0 0 91366 0 0:00:04 0:00:04 --:--:-- 101k
MacBookUniBody:JAVASCRIPT jorge$ tar xzf autoconf-2.13.tar.gz
MacBookUniBody:JAVASCRIPT jorge$ cd autoconf-2.13
MacBookUniBody:autoconf-2.13 jorge$ ./configure --prefix=/usr/local --program-suffix=213
creating cache ./config.cache
checking for gm4... /usr/bin/gm4
checking for mawk... no
@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');
@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 / 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');