Skip to content

Instantly share code, notes, and snippets.

View alessioalex's full-sized avatar

Alexandru Vlăduţu alessioalex

View GitHub Profile
@alessioalex
alessioalex / shortcut.js
Created March 7, 2016 11:26
Related to https://twitter.com/timferro/status/705189339890843648 - notify when the user types in a certain word / sequence of chars
function shortcut(shortcutKeys, callback) {
var code = shortcutKeys.toString();
var keys = [];
document.addEventListener('keydown', function(evt) {
keys.push(evt.keyCode);
if (keys.length > shortcutKeys.length) {
keys.shift();
}
@alessioalex
alessioalex / serially-iterating-array.js
Last active February 9, 2016 08:59
Serially Iterating An Array, Asynchronously
function run(steps, data, done) {
// our recursion exit strategy:
// no items left? we're done.
if (steps.length === 0) {
return done();
}
// get the first item in the array
var step = steps.shift();
/**
* POST to create a new user.
*/
exports.create = function *(){
var body = yield parse(this);
// password
var pass = body.password;
assert(pass, 400, 'password is required');

Virtual DOM and diffing algorithm

There was a [great article][1] about how react implements it's virtual DOM. There are some really interesting ideas in there but they are deeply buried in the implementation of the React framework.

However, it's possible to implement just the virtual DOM and diff algorithm on it's own as a set of independent modules.

{
"name": "my-app",
"version": "0.0.0",
"dependencies": {
"browserify": "~2.36.1",
"less": "~1.5.1"
},
"devDependencies": {
"watchify": "~0.4.1",
"catw": "~0.2.0"

node-websocketd

A lightweight node port of websocketd, originally written in go.

Usage

node-websocketd --port=8080 ./count.sh

Overview

We run multiple server processes in two data centers. Each process listens on two ports, one for HTTP and one for HTTPS. HTTPS is terminated by Apache prior to reaching node.js. HTTP goes directly from the client to node.js (through a master load balancer). We do not use clusters. We slice our physical servers into thin virtual machines running SmartOS, each with about 3GB of memory designed for a single node.js process.

Our node.js servers are hapi.js servers using the composer functionality and plugins architecture. We have three sets of plugins loaded: mobile web front end experience (single page app), legacy API reverse proxy, and monitoring.

We also serve original node.js services off another server zone which runs closed source plugins using hapi.

Analytics

/* Cranium MVC
* A minimalist MVC implementation written for
* demonstration purposes at my workshops
* http://addyosmani.com
* Copyright (c) 2012 Addy Osmani; Licensed MIT */
var Cranium = Cranium || {};
// Set DOM selection utility

6/23/2013

Last weekend I spent a few hours implementing the CouchDB document storage model on top of levelup in node.js.

Today I had a few spare hours in my Sunday and decided to implement the HTTP interface and compare the performance with Apach CouchDB. The benchmarks can be found here https://github.com/mikeal/couchup/tree/master/tests/benchmark

node compare-new-writes.js

Apache CouchDB 67140 writes
// server setup
var level = require('level')
var pre = require('level-prefix')
var LEVEL_PATH = '/tmp/multilevel-http'
// cleanup
require('rimraf').sync(LEVEL_PATH)
// levelDb instance
var db = level(LEVEL_PATH)
var multilevel = require('../')
var server = multilevel.server(db)