Skip to content

Instantly share code, notes, and snippets.

@hackape
hackape / latency.markdown
Created July 3, 2022 20:53 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@hackape
hackape / explanation.md
Created January 21, 2022 03:23 — forked from masak/explanation.md
How is git commit sha1 formed

Ok, I geeked out, and this is probably more information than you need. But it completely answers the question. Sorry. ☺

Locally, I'm at this commit:

$ git show
commit d6cd1e2bd19e03a81132a23b2025920577f84e37
Author: jnthn <jnthn@jnthn.net>
Date:   Sun Apr 15 16:35:03 2012 +0200

When I added FIRST/NEXT/LAST, it was idiomatic but not quite so fast. This makes it faster. Another little bit of masak++'s program.

@hackape
hackape / send_operator.js
Created April 13, 2021 05:31
JS. fake infix overload
/** might not be a good idea
* but it's fun
* you can overload infix operator in JS by overriding `.valueOf()` method.
The following binary operators coerce:
+ - * / %
& | ^ << >> >>>
< <= > >=
*/
@hackape
hackape / crypto-aes-gcm.js
Created March 24, 2021 09:02 — forked from chrisveness/crypto-aes-gcm.js
Uses the SubtleCrypto interface of the Web Cryptography API to encrypt and decrypt text using AES-GCM (AES Galois counter mode).
/**
* Encrypts plaintext using AES-GCM with supplied password, for decryption with aesGcmDecrypt().
* (c) Chris Veness MIT Licence
*
* @param {String} plaintext - Plaintext to be encrypted.
* @param {String} password - Password to use to encrypt plaintext.
* @returns {String} Encrypted ciphertext.
*
* @example
* const ciphertext = await aesGcmEncrypt('my secret text', 'pw');
@hackape
hackape / machine.js
Last active March 18, 2021 07:01
Generated by XState Viz: https://xstate.js.org/viz
const websocketMachine = Machine({
initial: '@closed',
states: {
'@closed': {},
'@connecting': {},
'@closing': {},
'@reconnecting': {},
'@connected': {},
'@disconnected': {},
},
@hackape
hackape / machine.js
Created February 26, 2021 08:09
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@hackape
hackape / README.md
Last active October 27, 2020 03:12
Find all combinations from given ordered choices.

Combinations

Find all combinations from given ordered choices.

Implementation

function getAllCombinations<T>(slotsChoices: T[][]) {
  const searchSpaceSizes = slotsChoices.map(choices => choices.length)
  const solutionSize = searchSpaceSizes.reduce((a, b) => a * b)
  const slots = new Array(solutionSize).fill(slotsChoices)
@hackape
hackape / checker_mod.ts
Last active January 26, 2023 15:18
Answer to Stack Overflow Question: "How can I see how TypeScript computes types?"
// https://github.com/microsoft/TypeScript/blob/ba5e86f1406f39e89d56d4b32fd6ff8de09a0bf3/src/compiler/checker.ts
// 1. add this line to ln:3
export const _conditionalTypes: any = {}
// 2. then replace ln:12303 to ln:12360
function trackConditionalType() {
// one time stuff