Skip to content

Instantly share code, notes, and snippets.

@weizhu
weizhu / test.js
Last active December 30, 2015 03:39
Compare the performance of generators written in JavaScript in node-fibers and native generator in Python. Unfortunately, it looks like generator creation in node-fiber is very expensive. On a AWS m1.small instance: the JS code takes 968ms while Python code take 5 ms.
var Fiber = require('fibers');
function inc(x) {
while (true) {
x = x + 1;
Fiber.yield(x);
}
}
var list=[];
@weizhu
weizhu / gist:7729799
Last active December 29, 2015 21:19
Demonstrate performance problem with .future()
"use strict";
var Fiber = require('fibers');
var Future = require('fibers/future'), wait = Future.wait;
// This Client class implement a caching accessing layer (Redis or Memcached)
// that uses batching to optimize IO
function Client() {
this.scheduled = false;
this.queue = {
reqsArgs: [],
@weizhu
weizhu / gist:7613409
Created November 23, 2013 11:18
Testing performance of fiber future
"use strict";
var Fiber = require('fibers');
var Future = require('fibers/future'), wait = Future.wait;
var getByIdCb= function(id, cb) {
cb(null, 'foo');
}
var getById = Future.wrap(getByIdCb);
@weizhu
weizhu / app.js
Created November 23, 2013 11:04
Use scope to access context
app.get('/api', function(req, res) {
Sync(function() {
// Store current request in scope
Sync.scope.req = req;
var result = foo(req);
res.json(result);
});
});
var fs = require('fs');
function UInt32(str, pos) {
return (str.charCodeAt(pos++)) +
(str.charCodeAt(pos++) << 8) +
(str.charCodeAt(pos++) << 16) +
(str.charCodeAt(pos) << 24);
}
function UInt16(str, pos) {
@weizhu
weizhu / PhoneGap FB Connect hooks
Created January 13, 2012 17:54
Sample implementation for a PhoneGap FB Plugin that hook up necessary events from regular Facebook JS SDK so it override behavior for Auth and UI.
FB.Event.subscribe('FB.init.start',
function(options) {
if (options.nativeInterface) {
FB._nativeInterface = options.nativeInterface;
FB._nativeInterface.init(FB._apiKey);
}
});
FB.Event.subscribe('FB.auth.staticAuthCheck.start',
@weizhu
weizhu / getStackTrace
Created July 1, 2011 21:41
getStackTrace
Function.prototype.trace = function()
{
var trace = [];
var current = this;
while(current)
{
trace.push(current.signature());
current = current.caller;
}
return trace;