Skip to content

Instantly share code, notes, and snippets.

@xk
xk / walkADir.js
Created Dec 15, 2010
walkADir.js
View walkADir.js
// 2010-12-15 jorge@jorgechamorro.com
// Uses 2 threads
function walk (file, cb) {
var fs = require('fs');
var e= {path:file, q:[]};
var q= [e];
begin();
function begin () {
@xk
xk / debugger.js
Created Dec 18, 2010
debugger.js
View debugger.js
// 2010-12-16 jorge@jorgechamorro.com
// loopFreq.js displays event loop tick period in ms and KHz
// with debugger; slows down to a crawl !
var k_ms= 1e3;
setTimeout(function display () {
t= Date.now()-t;
console.log(["ctr: ",ctr, ", t:", t, "ms -> ", (ctr/t).toFixed(2), "KHz"].join(''));
setTimeout(display, k_ms);
@xk
xk / wowAndFlutter.js
Created Dec 31, 2010
wowAndFlutter.js
View wowAndFlutter.js
// 2010-12-31 jorge@jorgechamorro.com
// JavaScript ms clock's wow and flutter
var kSample= 1e3;
var res= new Array(kSample);
var ctr;
var st= Date.now()+ 2; //start time
while (Date.now() < st) ; //get in sync with ms clock
View snap-benchmark's pong implementation in node.js
function server (request, response) {
var uri = url.parse(request.url).pathname;
if(uri=='/pong') {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('PONG');
} else if ((match = uri.match(/^\/echo\/(.*)$/)) != null) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end(match[1]);
View takePhoto.js
(function () {
function photoOK (imageURI) {
console.log("**** photoOK");
console.log(imageURI);
}
function photoErr (mesage) {
console.log("**** photoErr");
View nodejs.js
var k= require('constants');
var fs = require('fs')
var b = new Buffer('AAAAAAAAAA');
fs.open('./f', k.O_WRONLY, 0666 , cb);
function cb (err, fd) {
fs.write(fd,b,0,10,10);
}
View test.js
function LinkedList() {
this._length = 0;
this._head = null;
this._tail = null;
}
LinkedList.prototype = {
add: function (data){
var node = { data: data, next: null};
if (this._length == 0) {
@xk
xk / rotate.js
Created Mar 4, 2011
phonegap:rotate.js
View rotate.js
<!DOCTYPE html>
<html>
<head>
<!-- Change this if you want to allow scaling -->
<meta name="viewport" content="width=default-width; user-scalable=no">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>gira</title>
@xk
xk / selfShadowingGetter.js
Created Mar 8, 2011
selfShadowingGetter.js
View 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;
}
View expando-url.js
// 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)
You can’t perform that action at this time.