Skip to content

Instantly share code, notes, and snippets.

View alexewerlof's full-sized avatar
:octocat:

Alex Ewerlöf alexewerlof

:octocat:
View GitHub Profile
@alexewerlof
alexewerlof / fetch-response-header.js
Last active February 22, 2023 16:59
converting fetch response headers to a plain javascript object
const fetch = require('node-fetch')
const am = require('am')
async function main() {
const url = 'https://www.example.com'
const response = await fetch(url)
const responseHeaders = Object.fromEntries(resp.headers.entries())
console.dir(responseHeaders)
return responseHeaders
}
@alexewerlof
alexewerlof / CacheFn
Created March 17, 2019 08:34
A typescript class that memoizes a single-argument function `(string) => T` with a limited cache size
export class CachedFn<T> {
private cache: {
[key: string]: T
} = {}
private keys: string[]
private currKeyIndex = 0
constructor(private fn: (arg: string) => T, private size: number = 100) {
this.keys = new Array(size)
@alexewerlof
alexewerlof / fib.js
Created December 30, 2018 21:04
A fast algorithm for computing the n-th number in the fibonacci series in Node.js
const N = 1000000
function fib(nth) {
if (nth < 3) {
return 1n
}
let a = b = 1n, c
for (let i = 3; i <= nth; i++) {
c = a + b
a = b, b = c