Skip to content

Instantly share code, notes, and snippets.

View brycebaril's full-sized avatar

Bryce Baril brycebaril

View GitHub Profile
@brycebaril
brycebaril / eventloop.js
Last active August 29, 2015 13:58
An interesting little demo to demonstrate some of the intricacies and changes with the Node.js event loop
var counter = 0
var start = Date.now()
function foo(from, when) {
console.log(counter, when, from, process._getActiveHandles().length, Date.now() - start)
counter++
var scheduledAt = counter
if (counter == 2 || counter == 25) {
function abc() {
return def();
}
function def() {
return abc.arguments[0] * 2;
}
abc(50); // >> 100
// so that's weird enough but:
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "require"

Y axis = stack depth X axis = percent of time spent in this function

If a function sits atop another, it was in its callchain and spent time in the callee. The more of a parent that is covered by the things it calls, the less it is doing and the more its children are doing.

This means the two main things (in my experience) to look for are:

  • plateaus -- functions where a lot of time was spent
  • shallow-sloped pyramids -- functions where time was spent up and down the callchain vs any isolated place

In the middle of the wide section you can see a function called "parseGif" which is where we'll focus. This particular flame graph was generated to analyze something inside there -- for the most part I don't care about the rest right now.

@brycebaril
brycebaril / nsolid.md
Created September 22, 2015 15:53
So you want to try N|Solid? Here's a simple playlist:

Trying N|Solid

  1. Download the N|Solid Runtime https://downloads.nodesource.com/
  2. tar -xf nsolid-v1.0...
  3. cd nsolid-v1.0...
  4. ./nsolid /path/to/your/app.js

Now let's get tricky

  1. NSOLID_SOCKET=8000 ./nsolid /path/to/your/app.js
@brycebaril
brycebaril / gist:4577164
Last active December 11, 2015 08:58 — forked from cxreg/gist:4577093
var async = require('async');
function one(cb) {
console.log("running one");
cb(null, function () { return "one" });
}
function two(cb) {
console.log("running two");
function three(callback){
#!/bin/bash
if [[ $1 != "-f" ]]; then
echo "### Dry-run mode, specify -f to actually perform deletes.";
fi;
for branch in $(git branch -r --merged origin/master | grep '\<origin/' | grep -v '\<origin/master\>');
do
if [[ -z $(git rev-list $branch --since '1 month') ]]; then
name=$(echo $branch | sed 's/^origin\///');
if [[ $1 = "-f" ]]; then
// Short module explanation // Something to gather my thoughts
// // I think this looks cool.
//
module.exports = the_exported_function // Modules are a function. Always.
//
module.exports.extra = extra // Additional API entry points if
module.exports.other = other // desired.
//
var util = require('util') // Other packages from npm or core
var assert = require('assert') // No comma-first due to lots of
var cluster = require('cluster');
var PORT = +process.env.PORT || 1337;
if (cluster.isMaster) {
// In real life, you'd probably use more than just 2 workers,
// and perhaps not put the master and worker in the same file.
cluster.fork();
cluster.fork();
cluster.on('disconnect', function(worker) {
> var redis = require("redis")
undefined
> var c = redis.createClient()
undefined
> c.blpop("Dispo", 0, console.log)
true
> null [ 'Dispo', 'foo' ]