Skip to content

Instantly share code, notes, and snippets.

View owenallenaz's full-sized avatar

Owen Allen owenallenaz

View GitHub Profile
@owenallenaz
owenallenaz / dnsCaller.js
Created January 13, 2017 17:53
Showing stalled file reads because of `dns.lookup`
var dns = require("dns");
var http = require("http");
var fs = require("fs");
var net = require("net");
dns.setServers(["127.0.0.1"]);
var makeCallLookup = function(cb) {
dns.lookup("testSlow.com", cb);
}
@owenallenaz
owenallenaz / loadModule.js
Last active June 29, 2017 20:41
Fast module hacking
// var fs = require("fs");
// var Module = require("module");
// var pathCache = JSON.parse(fs.readFileSync("/sv/test/pathCache.json").toString());
// Object.keys(pathCache).forEach(function(val, i) {
// Module._pathCache[val] = pathCache[val];
// });
// console.time("load");
// // var mongolayer = require("/sv/node_modules/npm/mongolayer/1/node_modules/mongolayer/");
@owenallenaz
owenallenaz / README.md
Created December 20, 2016 22:17
Faking a slow DNS server to show setTimeout not affected by dns resolution timing

node dnsLagDnsServer.js

node dnsLagServer.js

node dnsLag.js

--- dnsLag.js should timeout if the timeout was affected

@owenallenaz
owenallenaz / results.txt
Last active October 31, 2016 08:17
Margin of error in polls with introduced bias
------------
test { populationSize: 100000000,
sampleSize: 1000,
bias: 50,
marginOfError: 0.030990321069650117 }
inMarginOfError { yes: 940, no: 60 }
predictedElections { wins: 513, ties: 34, losses: 453 }
------------
test { populationSize: 100000000,
sampleSize: 1000,
@owenallenaz
owenallenaz / README.md
Created July 27, 2016 19:26
process.nextTick is required for proper domain entrance and preventing leaking of domains

In the following test code there are two things to look for.

"in cb 0 0 1" means that it is in the callback after the call to mongodb, and the numbers are "expectedNum actualNum stackSize". The expectedNum and actualNum match, and they do. The stackSize should always be 1. In the event the stackSize is not 1 it means that the domains are not being properly entered and exited. If the stackSize grows domains are leaking.

Another important marker is the "after exit" and "end call" blocks, they should always be undefined. In the event that they are not undefined it means that a domain is lingering after exit, which results in subsequent code being in a domain it shouldn't, as well as a possible memory leak.

From the test file it appears that both setImmediate and process.nextTick() work.

@owenallenaz
owenallenaz / testPool.js
Created January 3, 2016 20:11
Testing worker based socket pooling mongodb
var async = require("async/");
var mongodb = require("mongodb");
var assert = require("assert");
mongodb.MongoClient.connect("mongodb://127.0.0.1/test", {}, function(err, conn) {
if (err) { throw err; }
var collection = conn.collection("test");
var newDocs = [];
@owenallenaz
owenallenaz / result.txt
Last active September 29, 2015 21:48
Comparing object_sizeof and JSON.stringify
npm: 34ms
json: 7ms
317780 307781
@owenallenaz
owenallenaz / test.md
Created January 13, 2015 01:34
Multiple code blocks bring Dillinger to a halt.
foo
foo
foo

Example showing async.waterfall which functions semantically the same as promises.

<script src="http://cdnjs.cloudflare.com/ajax/libs/async/1.22/async.min.js"></script>

<script>
	// waterfall wrapper to try/catch each method in a waterfall
	var catchWaterfall = function(arr, cb) {
		var calls = [];
		arr.forEach(function(val) {
@owenallenaz
owenallenaz / caught.js
Last active March 5, 2022 15:30
Showing that nodeJS domains do not catch synchronous errors. Hence the common practice is to enclose them in process.nextTick()
var domain = require("domain");
var d = domain.create();
d.on("error", function() {
console.log("domain caught");
});
try {
d.run(function() {
process.nextTick(function() {