Skip to content

Instantly share code, notes, and snippets.

Avatar

Bryce Baril brycebaril

View GitHub Profile
@brycebaril
brycebaril / output.md
Last active Feb 23, 2020
process.nextTick vs setImmediate
View output.md

@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 Apr 13, 2017 — 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.
View binding.gyp
{
"targets": [{
"target_name": "addon",
"sources": [ "main.cc" ]
}]
}
@brycebaril
brycebaril / README.md
Last active Jun 23, 2016
another user queue example, similar to how the Redis protocol works
View README.md

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 Dec 18, 2013
poor-man's multibuffer-stream
View s.js
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()
}))
View baleet.js
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 Oct 29, 2013
Demonstrating how highWaterMark works with objectMode streams
View hwm.js
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 Oct 10, 2013
Run setup.js to create the file the other two use
View protobuf.js
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) {
View 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 Dec 21, 2015
Zip two ordered time streams. A full version of this can be found here: https://github.com/brycebaril/node-sosj
View align.js
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 Aug 9, 2013
eeevil! Ok, not too evil...
View entry.js
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