Skip to content

Instantly share code, notes, and snippets.

View brycebaril's full-sized avatar

Bryce Baril brycebaril

View GitHub Profile
@brycebaril
brycebaril / binding.gyp
Created April 13, 2017 00:01 — forked from trevnorris/binding.gyp
Example of returning all values from uv_rusage() as tuples w/ no overhead. Values are only valid until getrusage() is called again.
{
"targets": [{
"target_name": "addon",
"sources": [ "main.cc" ]
}]
}
@brycebaril
brycebaril / README.md
Last active June 23, 2016 03:22
another user queue example, similar to how the Redis protocol works

requests come in e.g. /foo

replies come out upper-cased as /FOO

however, they are sent over to db.js to get upper-cased, and it replies two at once, concatenated with \n to delimit

when a reply comes back from db.js it sends both queued replies back to the HTTP clients at the same time, i.e. the first will wait until the second comes in.

@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 / output.md
Last active February 23, 2020 22:35
process.nextTick vs setImmediate

@mafintosh asks: "Does anyone have a good code example of when to use setImmediate instead of nextTick?"

https://twitter.com/mafintosh/status/624590818125352960

The answer is "generally anywhere outside of core".

process.nextTick is barely asynchronous. Flow-wise it is asynchronous, but it will trigger before any other asynchronous events can (timers, io, etc.) and thus can starve the event loop.

In this script I show a starved event loop where I just synchronously block, use nextTick and setImmediate

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.

{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "require"
function abc() {
return def();
}
function def() {
return abc.arguments[0] * 2;
}
abc(50); // >> 100
// so that's weird enough but:
@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) {
@brycebaril
brycebaril / linesplit.js
Created March 27, 2014 19:53
Split a file into a file per line of the input file.
var fs = require("fs")
var split = require("binary-split")
var terminus = require("terminus")
var path = require("path")
var input = process.argv[2]
var dir = path.dirname(input)
var basename = path.basename(input)
var count = 0

Keybase proof

I hereby claim:

  • I am brycebaril on github.
  • I am brycebaril (https://keybase.io/brycebaril) on keybase.
  • I have a public key whose fingerprint is 602C 9953 7442 29B1 B9E2 DFD8 9425 9F4D 2F09 DCCC

To claim this, I am signing this object: