Skip to content

Instantly share code, notes, and snippets.

Avatar

Will Keleher KelWill

View GitHub Profile
View async_local_storage_middleware.js
const { AsyncLocalStorage } = require("async_hooks");
const storageContext = new AsyncLocalStorage();
const middlewares = [];
function addMiddleware(callback) {
middlewares.push(callback);
}
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@KelWill
KelWill / include_benchmark.js
Last active Oct 22, 2021
includes is so fast on sorted arrays!
View include_benchmark.js
const Benchmark = require("benchmark");
const { ObjectId } = require("bson");
const _ = require("lodash");
const SIZES = [10, 40, 100, 1_000, 10_000, 100_000, 1_000_000];
for (const isRandom of [false, true]) {
for (let i = 0; i < SIZES.length; i++) {
const size = SIZES[i];
let docs = new Array(size).fill(0).map(() => new ObjectId().toHexString());
@KelWill
KelWill / concat_vs_push.js
Created Jun 14, 2021
How much slower is Array.concat compared to Array.push
View concat_vs_push.js
const Benchmark = require("benchmark");
const _ = require("lodash");
const suite = new Benchmark.Suite();
const TIMES = 10000;
suite
.add("pushing", function () {
const docs = [];
for (let i = 0; i < TIMES; i++) {
docs.push(new Date());
@KelWill
KelWill / download_atlas_logs.sh
Created Jun 10, 2021
script to download atlas logs. requires "project owner" access
View download_atlas_logs.sh
#!/bin/sh
set -eo pipefail
# Group ID is synonymous with projectId (in the ui)
GROUP_ID=""
# Configuration -- https://cloud.mongodb.com/v2/${GROUP_ID}/#access/apiKeys
# Note: the API key requires "Project owner" access in order to download the logs
if [ -z "$ATLAS_PUBLIC_KEY" ] || [ -z "$ATLAS_API_KEY" ]; then