Skip to content

Instantly share code, notes, and snippets.

View heapwolf's full-sized avatar
🕷️
Vault of the Black Spiders

heapwolf heapwolf

🕷️
Vault of the Black Spiders
View GitHub Profile
@heapwolf
heapwolf / pvsp.md
Last active February 18, 2018 18:39
promise vs promise
  pa x 630 ops/sec ±1.74% (76 runs sampled)
  pb x 7.28 ops/sec ±1.36% (38 runs sampled)
let Benchmark = require('benchmark')

let suite = new Benchmark.Suite()

The reason your tests is junk is because you are accidentally introducing all kinds of other variables into the environment (just like your other test, when you reverse it, it favors then-ables), too many side effects. Let the framework handle cycles, that's part of its purpose. Also, the end goal isn't for then-ables to out-perform promises, it's just to demonstrate a neat alternative way to create things that can be awaited.

To placate your placation, here is the same test with less noise...

p x 0.86 ops/sec ±0.31% (9 runs sampled)
t x 0.86 ops/sec ±0.16% (9 runs sampled)
function mkPromise(k) {
	return new Promise(resolve => setTimeout(() => resolve(k), 1))
}

function mkThenable(k) {
  return {
    then(done) { setTimeout(() => done(k), 1) }
  };
}
@heapwolf
heapwolf / benchmark.js
Created February 15, 2018 23:06
Benchmark for then-able vs promise
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
suite.on('cycle', event => console.log(String(event.target)))
const add = (name, fn) => suite.add(name, fn, { defer: true })
add('p', complete => {
async function test () {
const p = new Promise((resolve, reject) => {
@heapwolf
heapwolf / example2.js
Created January 26, 2018 15:33
scratches
async function main () {
const p = await fetch('https://api.github.com/users/zeke')
const j = await p.json()
console.log(j)
document.body.innerHTML = `
<style>
body {
padding: 20px 10px;
//
// CSS
//
const style = `
* { box-sizing: border-box; }
body { margin: 0 }
canvas {
width: 100%;
height: 100%;

Keybase proof

I hereby claim:

  • I am 0x00a on github.
  • I am 0x00a (https://keybase.io/0x00a) on keybase.
  • I have a public key whose fingerprint is ADDF 4548 FE3F 0087 758C C300 5BAD AA9B 3A31 9032

To claim this, I am signing this object:

#
# After installing Node.js, Get the SDK
#
curl https://s3.amazonaws.com/mozilla-games/emscripten/releases/emsdk-portable.tar.gz ~/Downloads/emsdk-portable.tar.gz
tar -xzf ~/Downloads/emsdk-portable.tar.gz ~/emscripten
cd ~/emscripten
#
# Fetch the latest registry of available tools.
#
@heapwolf
heapwolf / block.js
Created January 15, 2018 20:56
"early return" like behavior by breaking from a labeled block.
foo: {
const { err, res } = await request('https://quxx.ht')
if (err) {
// handle error
break foo
}
// handle success
}
@heapwolf
heapwolf / sh.js
Created January 13, 2018 08:38
A small bikeshed
//
// target.on(type, listener[, options])
//
EventTarget.prototype.on = EventTarget.prototype.addEventListener
//
// target.once(type, listener[, options])
//
EventTarget.prototype.once = function (type, listener, options = {}) {