Skip to content

Instantly share code, notes, and snippets.

View brycebaril's full-sized avatar

Bryce Baril brycebaril

View GitHub Profile
@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

@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 / 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) {
this.push(mb.pack([chunk]))
callback()
}))
@brycebaril
brycebaril / baleet.js
Created November 7, 2013 23:50
BALEET
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++) {
client.del(keys[i])
}
@brycebaril
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
this.push(record)
@brycebaril
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
brycebaril / README.md
Created September 7, 2013 04:38 — forked from rvagg/README.md

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.

Prerequisites:

  • Raspberry Pi
  • Kindle Paperwhite freed from its locked down state (jailbroken) http://www.mobileread.com/forums/showthread.php?t=198446
    • 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 mobileread.com forums and wiki.
@brycebaril
brycebaril / align.js
Last active December 21, 2015 23:18
Zip two ordered time streams. A full version of this can be found here: https://github.com/brycebaril/node-sosj
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
brycebaril / entry.js
Created August 9, 2013 00:27
eeevil! Ok, not too evil...
require("pipechain").install()
var spigot = require("stream-spigot") // or whatever you like to create streams
var s = spigot(["a", "b", "c", "\n"])
+s
require("./other.js")
// console will read abc