The Daily WTF
Odette’s company uses a popular video conferencing solution for browser use. In the base library, there’s a handy-dandy class called ObjectStorage, which implements an in-memory key/value store entirely in TypeScript/JavaScript.
“Wait,” you ask, “isn’t a JavaScript in-memory, key/value store just… an object? A map, if you’re being pedantic?”
Yes, of course, but have you thought about ways to make JavaScript’s maps/objects worse?
export class ObjectStorage { private _storage: { [k: string]: any; } = {}; private _counter = 0; public set(key: string, value: any): void { this._storage[key] = value; } public push(value: any): string { const key = this._getNextUniqueKey(); this._storage[key] = value; return key; } public get(key: string): any { return this._storage[key]; } public get keys(): string[] { return Object.keys(this._storage); } public get values(): any[] { return Object.values(this._storage); } private _getNextUniqueKey() { if (Object.keys(this._storage).includes(this._counter.toString())) { this._counter++; return this._getNextUniqueKey(); } return (this._counter++).toString(); } }
So, yes, this does just
To read the full article click on the 'post' link at the top.