Files
zulip/static/js/lazy_set.js
Priyank Patel 16a3d444fd lazy_set: Move set and array properties to the data field.
The data field will be a union type when it is converted to typescript.
This approach allows us to avoid introducing additional type check for
both of the properties.
2022-01-18 13:24:02 -08:00

91 lines
1.9 KiB
JavaScript

import * as blueslip from "./blueslip";
export class LazySet {
/*
This class is optimized for a very
particular use case.
We often have lots of subscribers on
a stream. We get an array from the
backend, because it's JSON.
Often the only operation we need
on subscribers is to get the length,
which is plenty cheap as an array.
Making an array from a set is cheap
for one stream, but it's expensive
for all N streams at page load.
Once somebody does an operation
where sets are useful, such
as has/add/delete, we convert it over
to a set for a one-time cost.
*/
constructor(vals) {
this.data = {
arr: vals,
set: undefined,
};
}
keys() {
const {data} = this;
if (data.set !== undefined) {
return data.set.keys();
}
return data.arr.values();
}
_make_set() {
if (this.data.set !== undefined) {
return;
}
this.data = {
arr: undefined,
set: new Set(this.data.arr),
};
}
get size() {
const {data} = this;
if (data.set !== undefined) {
return data.set.size;
}
return data.arr.length;
}
map(f) {
return Array.from(this.keys(), f);
}
has(v) {
this._make_set();
const val = this._clean(v);
return this.data.set.has(val);
}
add(v) {
this._make_set();
const val = this._clean(v);
this.data.set.add(val);
}
delete(v) {
this._make_set();
const val = this._clean(v);
return this.data.set.delete(val);
}
_clean(v) {
if (typeof v !== "number") {
blueslip.error("not a number");
return Number.parseInt(v, 10);
}
return v;
}
}