Skip to content

Instantly share code, notes, and snippets.

Avatar

Mathias Buus mafintosh

View GitHub Profile
@mafintosh
mafintosh / promise-unhandled-why.js
Last active Jul 14, 2021
promise-unhandled-why.js
View 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) {
View index.html
<!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>
View sodium-benchmark.txt
╔════════════════════════════════════════════════════════╤═════════╤═══════════════════╤═══════════╤═════════════════════════╗
║ 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
View gist:996d820ad8fec6f3369ab00251f40358
╔═════════════════════════════════════════════╤═════════╤═══════════════════╤═══════════╤═════════════════════════╗
║ 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
View test.js
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')
View test.js
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')
View passthrough-writable.js
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))
}
View process-queue.js
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)
View promise-hell.js
const p = Promise.reject()
p.catch(() => {})
function destroy () {
return p
}
const another = Promise.reject()
View db.json
{
"since": 7380137,
"addresses": [
"0x8c6b04da932d1151e0e1c65f3d8b12a971e02b17",
"0xd04a779f681014832013a5cc58bfafafa26cbaf5",
"0x439aa60d35338b01e7ce16b98469c26dd8a79b52",
"0x904224d70521a09aC117CD499F6B4A68aBc5B852"
],
"transactions": [
{