Skip to content

Instantly share code, notes, and snippets.

@chaoran
chaoran / fib.js
Created September 23, 2013 16:22
A fibonacci number calculator that demonstrates the performance loss of invoking process.nextTick multiple times.
function fib(n, callback) {
if (n < 2) return process.nextTick(function() {
callback(1);
});
var results = [];
function done(ret) {
results.push(ret);
if (results.length === 2)
@chaoran
chaoran / map.js
Created March 28, 2013 17:20
Map a user_id to a mysql box. It chooses the same shard for a user even if the number of MySql boxes changes
var bounds = [0]
var hashs = [inital_number_of_box]
function map(user_id) {
for (var i = bounds.length-1; i >= 0; --i) {
if (user_id > bounds[i]) {
var shard_id = user_id % hashs[i]
return shard_id
}
}