Skip to content

Instantly share code, notes, and snippets.

// Takes any async lib that uses callback based signatures and converts
// the specified names to continuable style and returns the new library.
exports.convert = function (lib, names) {
var newlib = {};
Object.keys(lib).forEach(function (key) {
if (names.indexOf(key) < 0) {
return newlib[key] = lib[key];
}
newlib[key] = function () {
var args = Array.prototype.slice.call(arguments);
@nanha
nanha / gist:1385106
Created November 22, 2011 07:28
node.js CRC32
var crypto = require('crypto');
/**
* Calculates the hash/checksum of a string. Default algorithm is MD5.
*
* @param {String} str
* @param {String} algorithm
* @return {String} checksum
* @api public
*/
@nanha
nanha / gist:1413792
Created December 1, 2011 04:58
Async foreach
Array.prototype.asyncEach = function(iterator) {
var list = this,
n = list.length,
i = -1,
calls = 0,
looping = false;
var iterate = function() {
calls -= 1;
i += 1;
@nanha
nanha / BinaryModules.js
Created December 6, 2011 00:23 — forked from brikis98/BinaryModules.js
Node.js performance tips
// Use built in or binary modules
var crypto = require('crypto');
var hash = crypto.createHmac("sha1",key).update(signatureBase).digest("base64");
@nanha
nanha / actionQueue.js
Created December 13, 2011 02:03 — forked from voidfiles/actionQueue.js
actionQueue
(function(A) {
var registered_ids = {},
id_to_module_map = {},
interim_actions = {},
cleanup_actions = {},
clicked_ids = {},
queueing = {};
function register_id(id, callbacks, required_module) {
id = id.replace('*', '.*');
@nanha
nanha / gist:1483664
Created December 16, 2011 00:12
git 프롬프트 리눅스 버젼
c_cyan="\033[36m"
c_red="\033[31m"
c_green="\033[32m"
c_pink="\033[35m"
c_sgr0="\033[0m"
parse_branch ()
{
if git rev-parse --git-dir >/dev/null 2>&1
then
@nanha
nanha / node console
Created January 25, 2012 13:02 — forked from benmonty/node console
simple node.js v8 example
> var p = require('./point');
> var o = new p.Point();
> o.setX(6);
> o.setY(9);
> console.log(o.get());
{ x: 6, y: 9 }
@nanha
nanha / b2.js
Created January 27, 2012 07:55
var startFast;
var startSlow;
var size = 1000000; // million
var loopCount = 0;
var setTimeoutTest = function () {
loopCount++;
if ( loopCount == ( size - 1 ) ) {
console.log( 'setTimeout result: ' + ( new Date() - startFast ) + 'ms' );
@nanha
nanha / nextTick.js
Created January 27, 2012 07:58 — forked from xk/nextTick.js
nextTick.js
(function (ctr,t) {
function inc () { ctr++ }
function display (ntps,ntpsStr,i,lps,lpsStr,ratioStr) {
ntps= ctr*1e3/(Date.now()-t); //nextTicks per second
ntpsStr= ", nextTicksPerSecond: "+ ntps.toFixed(1);
ctr= 0, i= 100e6, t= Date.now();
while (i--) { inc() }
@nanha
nanha / application.js
Created January 27, 2012 13:03 — forked from fabware/application.js
socket.io application and haproxy configuration
var express = require('express');
var redis = require('redis');
const serverType = process.argv[2];
const serverHost = process.argv[3];
const serverPort = parseInt(process.argv[4]);
const redisPort = 6379;
const redisHost = '127.0.0.1';