Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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());
if (isRandom) {
docs = _.orderBy(docs, () => _.random(-1, 1));
}
const keyedDocs = _.keyBy(docs);
const docSet = new Set(docs);
const suite = new Benchmark.Suite();
suite
.add("object access", function () {
const toGet = docs[_.random(0, size - 1)];
keyedDocs[toGet];
})
.add("set", function () {
const toGet = docs[_.random(0, size - 1)];
docSet.has(toGet);
})
.add("array", function () {
const toGet = docs[_.random(0, size - 1)];
docs.includes(toGet);
})
.on("cycle", (event) => {
console.log(`\t ${size}`, String(event.target));
})
.on("complete", function () {
console.log(
`Fastest for ${isRandom ? "random" : "ordered"} ${size} is ` +
this.filter("fastest").map("name")
);
console.log("\n\n");
})
.run({ async: false });
}
}
/*
10 object access x 25,715,807 ops/sec ±0.70% (94 runs sampled)
10 set x 18,422,013 ops/sec ±0.85% (96 runs sampled)
10 array x 28,105,533 ops/sec ±0.88% (89 runs sampled)
Fastest for ordered 10 is array
40 object access x 20,175,473 ops/sec ±0.90% (92 runs sampled)
40 set x 19,632,409 ops/sec ±0.73% (92 runs sampled)
40 array x 27,999,865 ops/sec ±1.09% (94 runs sampled)
Fastest for ordered 40 is array
100 object access x 21,842,706 ops/sec ±0.70% (95 runs sampled)
100 set x 18,283,045 ops/sec ±0.78% (95 runs sampled)
100 array x 27,860,691 ops/sec ±1.03% (94 runs sampled)
Fastest for ordered 100 is array
1000 object access x 20,026,184 ops/sec ±0.82% (92 runs sampled)
1000 set x 14,656,626 ops/sec ±1.37% (89 runs sampled)
1000 array x 23,392,774 ops/sec ±1.31% (90 runs sampled)
Fastest for ordered 1000 is array
10000 object access x 14,367,754 ops/sec ±0.58% (93 runs sampled)
10000 set x 12,213,161 ops/sec ±0.51% (95 runs sampled)
10000 array x 28,067,504 ops/sec ±0.94% (92 runs sampled)
Fastest for ordered 10000 is array
100000 object access x 9,106,145 ops/sec ±1.55% (93 runs sampled)
100000 set x 7,918,164 ops/sec ±0.92% (91 runs sampled)
100000 array x 78,374 ops/sec ±58.42% (86 runs sampled)
Fastest for ordered 100000 is object access
1000000 object access x 3,296,346 ops/sec ±1.05% (93 runs sampled)
1000000 set x 2,076,943 ops/sec ±1.24% (91 runs sampled)
1000000 array x 490 ops/sec ±3.50% (40 runs sampled)
Fastest for ordered 1000000 is object access
10 object access x 24,869,178 ops/sec ±0.80% (91 runs sampled)
10 set x 19,181,356 ops/sec ±0.98% (92 runs sampled)
10 array x 27,691,570 ops/sec ±0.99% (90 runs sampled)
Fastest for random 10 is array
40 object access x 27,450,529 ops/sec ±1.30% (86 runs sampled)
40 set x 12,483,804 ops/sec ±1.30% (91 runs sampled)
40 array x 16,590,091 ops/sec ±0.92% (92 runs sampled)
Fastest for random 40 is object access
100 object access x 31,040,999 ops/sec ±1.24% (87 runs sampled)
100 set x 12,386,070 ops/sec ±1.14% (91 runs sampled)
100 array x 16,389,384 ops/sec ±1.22% (91 runs sampled)
Fastest for random 100 is object access
1000 object access x 25,365,248 ops/sec ±1.52% (88 runs sampled)
1000 set x 11,275,049 ops/sec ±1.51% (91 runs sampled)
1000 array x 16,294,268 ops/sec ±1.23% (88 runs sampled)
Fastest for random 1000 is object access
10000 object access x 16,907,288 ops/sec ±1.47% (89 runs sampled)
10000 set x 8,905,038 ops/sec ±1.47% (91 runs sampled)
10000 array x 14,777,156 ops/sec ±1.19% (91 runs sampled)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment