This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fails as soon as one of the map functions fails | |
const results = await Promise.all(array.map(asynMapFunction)) | |
// Continues running even if one map function fails | |
const results = await Promise.allSettled(array.map(asynMapFunction)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets the index of the element in an array that corresponds to the given percentile | |
* @param {number} arrLength the length of the array that we want to calculate its percentile | |
* @param {number} p the percentile in the range [0..100] inclusive | |
* @returns index of the array element that corresponds to the given percentile | |
*/ | |
export function percentileIndex(arrLength, p) { | |
const maxPossibleIndex = arrLength - 1 | |
return Math.ceil(maxPossibleIndex * p / 100) | |
} |