Skip to content

Instantly share code, notes, and snippets.

@lemire
Created May 8, 2023 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemire/6062fbde9aae4c85e445e996ab047eb2 to your computer and use it in GitHub Desktop.
Save lemire/6062fbde9aae4c85e445e996ab047eb2 to your computer and use it in GitHub Desktop.
roaring vs set
const Benchmark = require('benchmark');
const { TypedFastBitSet } = require('typedfastbitset');
const { RoaringBitmap32 } = require('roaring/RoaringBitmap32');
// Set utils
function union(set1, set2) {
return new Set([...set1, ...set2]);
}
function difference(set1, set2) {
const results = new Set();
for (const value of set1) {
if (!set2.has(value)) {
results.add(value);
}
}
for (const value of set2) {
if (!set1.has(value)) {
results.add(value);
}
}
return results;
}
function intersection(set1, set2) {
const [smaller, larger] = set1.size < set2.size ? [set1, set2] : [set2, set1];
const result = new Set();
for (const value of larger) {
if (smaller.has(value)) {
result.add(value);
}
}
return result;
}
const smallgap = 3;
const N = 1024;
function bench() {
const tb = new TypedFastBitSet();
const s = new Set();
const r = new RoaringBitmap32();
const arbitraryNumber = Math.floor(Math.random() * N) * smallgap + 5;
for (let i = 0; i < N; i++) {
const n = smallgap * i + 5;
tb.add(n);
s.add(n);
r.add(n);
}
const suite = new Benchmark.Suite();
suite
.add('TypedFastBitSet has', () => {
return tb.has(arbitraryNumber);
})
.add('Set has', () => {
return s.has(arbitraryNumber);
})
.add('roaring has', () => {
return r.has(arbitraryNumber);
})
.add('TypedFastBitSet union', () => {
const tb2 = new TypedFastBitSet([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return tb.new_union(tb2);
})
.add('Set union', () => {
const s2 = new Set([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return union(s, s2);
})
.add('roaring union', () => {
const r2 = new RoaringBitmap32([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return RoaringBitmap32.or(r, r2);
})
.add('TypedFastBitSet difference', () => {
const tb2 = new TypedFastBitSet([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return tb.difference(tb2);
})
.add('Set difference', () => {
const s2 = new Set([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return difference(s, s2);
})
.add('roaring difference', () => {
const s2 = new RoaringBitmap32([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return RoaringBitmap32.andNot(r, s2);
})
.add('TypedFastBitSet intersection', () => {
const tb2 = new TypedFastBitSet([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return tb.intersection(tb2);
})
.add('Set intersection', () => {
const s2 = new Set([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return intersection(s, s2);
})
.add('Roaring intersection', () => {
const s2 = new RoaringBitmap32([
230, 2130, 23012, 3020, 210310, 23902894, 3848, 2342, 8585, 50404,
]);
return RoaringBitmap32.and(r, s2);
})
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.run({ async: false });
}
bench();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment