Skip to content

Instantly share code, notes, and snippets.

View brycebaril's full-sized avatar

Bryce Baril brycebaril

View GitHub Profile
brycebaril /
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?"

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

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": [ "" ]
brycebaril /
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 / s.js
Created December 18, 2013 05:33
poor-man's multibuffer-stream
var spigot = require("stream-spigot")
var through2 = require("through2")
var terminus = require("terminus")
var mb = require("multibuffer")
spigot(["aaaaaaaaaaa", "bbbbbbbbbbbb", "cccccccccccccccc"])
.pipe(through2(function (chunk, encoding, callback) {
brycebaril / baleet.js
Created November 7, 2013 23:50
var client = require("redis").createClient()
var pattern = process.argv[2]
if (!pattern) throw new Error("Read codez for usage")
client.keys(pattern, function (err, keys) {
for (var i = 0; i < keys.length; i++) {
brycebaril / hwm.js
Created October 29, 2013 05:31
Demonstrating how highWaterMark works with objectMode streams
var spigot = require("stream-spigot")
var through2 = require("through2")
var HWM = 10000
var limit = 40000
var count = 0
function gen() {
var record = (count++ < limit) ? {record: count} : null
brycebaril / protobuf.js
Created October 10, 2013 05:36
Run setup.js to create the file the other two use
var probuf = require("leveljs-coding/protobuf")
var fs = require("fs")
var concat = require("concat-stream")
var input = fs.createReadStream("buffs")
input.pipe(concat(function (buffer) {
var offset = 0
var blen = buffer.length
while (offset < blen) {
brycebaril /
Created September 7, 2013 04:38 — forked from rvagg/

Work in progress, I'll write this up properly when I'm done.

Almost all credit goes to @maxogden for putting me on to this and pointing me in the right direction for each of these items.


  • Raspberry Pi
  • Kindle Paperwhite freed from its locked down state (jailbroken)
    • You have to downgrade your Kindle to 5.3.1 to install the current jailbreak; that's just a matter of getting the old version image, putting it on your Kindle via USB and telling it to install "upgrade". Then you put in the Jailbreak files, load the ebook and break.
  • Your kindle will be quick to detect an upgrade is available so it'll want to upgrade soon afterwards but the jailbreak will last but you have to reinstall the developer certificates so it's a bit of a pain but doable. Find all the instructions on the forums and wiki.
brycebaril / align.js
Last active December 21, 2015 23:18
Zip two ordered time streams. A full version of this can be found here:
module.exports = align
var Transform = require("stream").Transform
|| require("readable-stream/transform")
var inherits = require("util").inherits
var map = require("through2-map")
* align takes two objectMode streams and a sequence key and will create a stream
* aligning records from each stream resulting in a stream that emits record doublets
brycebaril / entry.js
Created August 9, 2013 00:27
eeevil! Ok, not too evil...
var spigot = require("stream-spigot") // or whatever you like to create streams
var s = spigot(["a", "b", "c", "\n"])
// console will read abc