Skip to content

Instantly share code, notes, and snippets.

View mafintosh's full-sized avatar

Mathias Buus mafintosh

View GitHub Profile
yrb9it8e6yzrxcxm48xbihewqh1zca9i93t3gc4scccgz11y6e69yr3ss1be6ksuacbopt66wrw4rtpq1k8gqihykshqqg1dznydnkjheafyotmf
#include <dlfcn.h>
static int mallocs_active = 0;
void *malloc(size_t size) {
void * (*real_malloc)(size_t);
real_malloc = dlsym(RTLD_NEXT, "malloc");
mallocs_active++;
void *ptr = real_malloc(size);
@mafintosh
mafintosh / promise-unhandled-why.js
Last active March 30, 2022 06:30
promise-unhandled-why.js
start()
async function start () {
const promises = [
new Promise((resolve, reject) => setTimeout(reject, 1000)),
Promise.reject(),
Promise.reject()
]
for (const a of promises) {
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<video src="custom://u" controls></video>
<!-- <video src="http://localhost:9999" controls></video> -->
</body>
╔════════════════════════════════════════════════════════╤═════════╤═══════════════════╤═══════════╤═════════════════════════╗
║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║
╟────────────────────────────────────────────────────────┼─────────┼───────────────────┼───────────┼─────────────────────────╢
║ crypto sha256 (prealloc) │ 10000 │ 370321.25 op/sec │ ± 4.65 % │ ║
║ crypto sha512 (prealloc) │ 1000 │ 382075.76 op/sec │ ± 0.94 % │ + 3.17 % ║
║ crypto blake2b512 (prealloc) │ 1000 │ 401530.47 op/sec │ ± 0.98 % │ + 8.43 % ║
║ crypto sha256 (no prealloc) │ 1000 │ 453419.74 op/sec │ ± 0.80 % │ + 22.44 % ║
║ crypto sha512 (no prealloc) │ 1000 │ 460150.51 op/sec │ ± 0.69 % │ + 24.26 % ║
║ crypto
╔═════════════════════════════════════════════╤═════════╤═══════════════════╤═══════════╤═════════════════════════╗
║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║
╟─────────────────────────────────────────────┼─────────┼───────────────────┼───────────┼─────────────────────────╢
║ crypto sha512 (prealloc) │ 10000 │ 340764.93 op/sec │ ± 6.40 % │ ║
║ crypto sha256 (prealloc) │ 1000 │ 357872.79 op/sec │ ± 0.63 % │ + 5.02 % ║
║ crypto blake2b512 (prealloc) │ 1000 │ 374023.05 op/sec │ ± 0.96 % │ + 9.76 % ║
║ crypto sha256 (no prealloc) │ 10000 │ 416877.14 op/sec │ ± 8.05 % │ + 22.34 % ║
║ crypto blake2b512 (no prealloc) │ 10000 │ 454022.65 op/sec │ ± 8.32 % │ + 33.24 % ║
║ crypto sha512 (no prealloc) │ 1000 │ 468638.04 op/sec │ ± 0.72 % │ + 37.5
const events = new require('events')
const { once } = events
const e = new events.EventEmitter()
main()
async function main () {
await once(e, 'foo')
console.log('foo was emitted')
const events = new require('events')
const { once } = events
const e = new events.EventEmitter()
main()
async function main () {
await once(e, 'foo')
console.log('foo was emitted')
const Duplexify = require('duplexify')
const clone = require('cloneable-readable')
class PassthroughWritable extends Duplexify {
constructor (ws) {
super()
const pt = new stream.PassThrough()
this.setWritable(stream.pipeline(pt, ws))
this.setReadable(clone(pt))
}
const queue = new Signal()
const done = new Signal()
const pending = []
async function runQueue () {
while (!pending.length && await queue.wait());
const current = pending
pending = []
await process(current)