Skip to content

Instantly share code, notes, and snippets.

@josephrocca
Last active May 19, 2024 21:35
Show Gist options
  • Save josephrocca/44e4c0b63828cfc6d6155097b2efc113 to your computer and use it in GitHub Desktop.
Save josephrocca/44e4c0b63828cfc6d6155097b2efc113 to your computer and use it in GitHub Desktop.
BigMap - wrapper to get past the ~16 million key limit on JavaScript Maps
// only covers a small subset of the Map api!
// haven't debugged yet!
class BigMap {
constructor(iterable) {
if(iterable) throw new Error("haven't implemented construction with iterable yet");
this._maps = [new Map()];
this._perMapSizeLimit = 14000000;
this.size = 0;
}
has(key) {
for(let map of this._maps) {
if(map.has(key)) return true;
}
return false;
}
get(key) {
for(let map of this._maps) {
if(map.has(key)) return map.get(key);
}
return undefined;
}
set(key, value) {
for(let map of this._maps) {
if(map.has(key)) {
map.set(key, value);
return this;
}
}
let map = this._maps[this._maps.length-1];
if(map.size > this._perMapSizeLimit) {
map = new Map();
this._maps.push(map);
}
map.set(key, value);
this.size++;
return this;
}
entries() {
let mapIndex = 0;
let entries = this._maps[mapIndex].entries();
return {
next: () => {
let n = entries.next();
if(n.done) {
if(this._maps[++mapIndex]) {
entries = this._maps[mapIndex].entries();
return entries.next();
} else {
return {done:true};
}
} else {
return n;
}
}
};
}
[Symbol.iterator]() {
return this.entries();
}
delete(key) { throw new Error("haven't implemented this yet"); }
keys() { throw new Error("haven't implemented this yet"); }
values() { throw new Error("haven't implemented this yet"); }
forEach(fn) { throw new Error("haven't implemented this yet"); }
clear() { throw new Error("haven't implemented this yet"); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment