Skip to content

Instantly share code, notes, and snippets.

View trevnorris's full-sized avatar

Trevor Norris trevnorris

View GitHub Profile
@trevnorris
trevnorris / fs-scan-n-read-bench.js
Last active March 11, 2017 00:25
Benchmark to show performance difference between fs sync and async calls
'use strict';
const MAX_DEPTH = 4;
const fs = require('fs');
const print = process._rawDebug;
const sep = require('path').sep;
const tmpdir = require('os').tmpdir() + sep + 'data-file-bench-' + genName();
const file_list = newDir();
var current_dir = file_list;
@trevnorris
trevnorris / intercept-stdio.cc
Last active February 27, 2017 21:16
Example of how to intercept messages to stdout/stderr
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#if defined(__unix__) || defined(__unix) || \
(defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif
@trevnorris
trevnorris / binding.gyp
Created December 1, 2016 22:28
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" ]
}]
}
@trevnorris
trevnorris / to-bind-or-not-to-bind.js
Created November 7, 2016 22:10
quick comparison doing a loop of nextTick()'s using different calling methods
'use strict';
const print = process._rawDebug;
const ITER = 1e6;
var t = process.hrtime();
(function runner(i) {
if (i > ITER) return printTime();
// Slowest
@trevnorris
trevnorris / tls_wrap-isalive-bad-pointer.diff
Created October 25, 2016 21:52
Fix when the reference of a TLSWrap instance is kept, but the StreamBase stream_ is free'd.
diff --git a/src/stream_base.h b/src/stream_base.h
index faddee8..3592975 100644
--- a/src/stream_base.h
+++ b/src/stream_base.h
@@ -146,10 +146,14 @@ class StreamResource {
const uv_buf_t* buf,
uv_handle_type pending,
void* ctx);
+ typedef void (*DestructCb)(void* ctx);
@trevnorris
trevnorris / multi-inheritance-nightmare.cc
Created October 20, 2016 23:32
log value from a Base class when multi-inheritance is involved
#include <cstdio>
#include <type_traits> // std::remove_reference
#ifdef USE_TR1_TYPE_TRAITS
template <typename T> using remove_reference = std::tr1::remove_reference<T>;
#else
template <typename T> using remove_reference = std::remove_reference<T>;
#endif
class Foo {
// map will store all the stack traces recorded at init().
var map = new Map();
var async_hooks = require('async_hooks');
async_hooks.createHook({ init }).enable();
function init(id, type, parentId) {
const obj = {};
// Capture the stack trace, omitting the call to init().
Error.captureStackTrace(obj, init);
@trevnorris
trevnorris / process_cpu.cc
Last active September 1, 2016 08:31
V8 + libuv API to return the CPU% utilization for given duration.
#include <v8.h>
#include <node.h>
#include <uv.h>
#include <assert.h>
#define MICROS_PER_SEC 1e6
static uint64_t cpu_speed_hz;
static uint64_t previous_total;
static uint64_t previous_time;
@trevnorris
trevnorris / json-v-buf.js
Created August 16, 2016 21:31
Benchmark testing parsing JSON data vs parsing a binary format from a Buffer
'use strict';
const print = process._rawDebug;
const ITER = 1e6;
const four_nulls = '\u0000\u0000\u0000\u0000';
// Mock object of only strings.
const test_obj = {
foobarbaz1: Math.random().toString(16).substr(2),
foobarbaz2: 'abcdefghijklmnop'.repeat(5),
@trevnorris
trevnorris / realpath-sync-bench.js
Last active July 22, 2016 22:48
Test realpath() and require() with and without cache between v4 and v6
'use strict';
// Clear disk cache with:
// sudo sh -c 'free && sync && echo 3 > /proc/sys/vm/drop_caches && free'
const fs = require('fs');
const Module = require('module');
const print = process._rawDebug;
const root_dir = process.cwd() + '/a_lot_of_files/';
print('writing files to', root_dir);