Skip to content

Instantly share code, notes, and snippets.

@KelWill
KelWill / download_atlas_logs.sh
Created June 10, 2021 23:21
script to download atlas logs. requires "project owner" access
#!/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
@KelWill
KelWill / concat_vs_push.js
Created June 14, 2021 16:25
How much slower is Array.concat compared to Array.push
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 / include_benchmark.js
Last active October 22, 2021 15:36
includes is so fast on sorted arrays!
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());
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));
const Benchmark = require("benchmark");
const { ObjectId } = require("bson");
const _ = require("lodash");
const size = 100;
let array = new Array(size).fill(0).map(() => new ObjectId().toHexString());
const keyedDocs = _.keyBy(array);
const set = new Set(array);
create or replace view digits as
select 0 as d union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9;
drop table if exists nums;
create table nums (n int auto_increment, primary key(n));
insert into nums values (), ();
insert into nums () select null from nums n1, nums n2, nums n3, nums n4, nums n5;
insert into nums () select null from nums n1, nums n2;
select all_nums.n
@KelWill
KelWill / benchark-arr-find.js
Last active December 1, 2023 22:16
benchmarking arr.find vs. dict lookup
const Benchmark = require("benchmark");
const { ObjectId } = require("bson");
const _ = require("lodash");
const casual = require("casual")
const size = 1000;
let array = new Array(size).fill(0).map(() => new ObjectId().toHexString());
const docs = array.map((_id) => ({
_id,