Skip to content

Instantly share code, notes, and snippets.

@mscdex
mscdex / bitCount.js
Created March 13, 2016 20:07
Fastest bit counting for 32-bit numbers in javascript
function bitCount(u) {
// https://blogs.msdn.microsoft.com/jeuge/2005/06/08/bit-fiddling-3/
const uCount = u - ((u >> 1) & 0o33333333333) - ((u >> 2) & 0o11111111111);
return ((uCount + (uCount >> 3)) & 0o30707070707) % 63;
}
@mathiasbynens
mathiasbynens / web-platform-status-links.md
Last active April 16, 2024 02:54
Web platform status links
@mscdex
mscdex / update_to_nan_v2.0.x.sh
Created August 30, 2015 05:55 — forked from thlorenz/update_to_nan_v2.0.x.sh
Script to update Node.js addons to work with nan 2.0.x and thus with iojs v3.x (gets you 90% there)
#!/bin/bash
replacements=(
"NanAsyncWorker/Nan::AsyncWorker"
"NanAsyncQueueWorker/Nan::AsyncQueueWorker"
"NanCallback/Nan::Callback"
"NanSetInternalFieldPointer/Nan::SetInternalFieldPointer"
"NanGetInternalFieldPointer/Nan::GetInternalFieldPointer"
"NanNewBufferHandle\\(([^;]+);/Nan::NewBuffer(\\1.ToLocalChecked();"
"(NanNew(<(v8::)?String>)?\\(\"[^\"]*\"\\))/\\1.ToLocalChecked()"
@CAFxX
CAFxX / persistent_pipes_linux.md
Last active January 4, 2024 04:32
Persistent pipes/circular buffers for Linux

📂 Persistent "pipes" in Linux

In a project I'm working on I ran into the requirement of having some sort of persistent FIFO buffer or pipe in Linux, i.e. something file-like that could accept writes from a process and persist it to disk until a second process reads (and acknowledges) it. The persistence should be both across process restarts as well as OS restarts.

AFAICT unfortunately in the Linux world such a primitive does not exist (named pipes/FIFOs do not persist

Notes:

  • Text in [[ ]] are the internal libuv function call.
  • Text in {{ }} are the Node functions that are affected.
  • Text in ( ) are notes about what is happening.
  • While the Windows event loop has minor variations, I don't believe any of those affect Node.

On process.nextTick():

@trevnorris
trevnorris / perf-flame-graph-notes.md
Last active December 24, 2023 05:25
Quick steps of how to create a flame graph using perf

The prep-script.sh will setup the latest Node and install the latest perf version on your Linux box.

When you want to generate the flame graph, run the following (folder locations taken from install script):

sudo sysctl kernel.kptr_restrict=0
# May also have to do the following:
# (additional reading http://unix.stackexchange.com/questions/14227/do-i-need-root-admin-permissions-to-run-userspace-perf-tool-perf-events-ar )
sudo sysctl kernel.perf_event_paranoid=0
@mscdex
mscdex / example.js
Created May 16, 2013 20:18
DataTables plug-in for displaying sort priorities in each sorted column header
$('#example').dataTable().fnSortPriorities();
@mscdex
mscdex / gist:5329227
Last active December 9, 2017 12:48
Why the IMAP protocol sucks
1. You cannot fetch .TEXT, .HEADER, etc. for parts that are not of type message/rfc822
2. Gotchas with multiple asynchronous requests
3. Response to partial body fetch does not include originally requested range, only the starting byte number
4. Fragmented fetch responses (servers are not required to collect all requested pieces of information for a particular message into a single response)
5. LIST can display child mailboxes before their parents
6. FETCHing a comma-separated list of messages (UIDs or seqnos) does not necessarily result in FETCH responses in that same order
7. Untagged FETCH responses containing FLAG updates can be sent for messages not requested *during* a FETCH request.
@mscdex
mscdex / readstream.js
Last active December 15, 2015 10:08
streams2 ReadStream that uses callbacks instead of a combination of 'readable' event listening and read()
var ReadableStream = require('stream').Readable;
var EMPTY_CALLBACK = function(n) {};
function ReadStream(cfg) {
if (!(this instanceof ReadStream))
return new ReadStream();
var self = this;
this._callbacks = [];
@mscdex
mscdex / gist:4463223
Last active December 10, 2015 16:48
node binary install one-liner for *nix
Linux 32-bit: curl http://nodejs.org/dist/latest/node-v0.10.29-linux-x86.tar.gz | tar zx --strip=1 -C /usr/local
Linux 64-bit: curl http://nodejs.org/dist/latest/node-v0.10.29-linux-x64.tar.gz | tar zx --strip=1 -C /usr/local