Skip to content

Instantly share code, notes, and snippets.

@phillippelevidad
Created November 3, 2023 10:03
Show Gist options
  • Select an option

  • Save phillippelevidad/d44aef576dd9f97e046987302992dbf8 to your computer and use it in GitHub Desktop.

Select an option

Save phillippelevidad/d44aef576dd9f97e046987302992dbf8 to your computer and use it in GitHub Desktop.
/**
* The UniquePropertySet class is designed to manage a collection of objects,
* ensuring that each object is unique based on its properties and values
* rather than its reference in memory.
*
* Beware, though, that this implementation is not optimized for performance,
* and that the JSON.stringify method does not handle non-serializable values
* consistently, such as functions, undefined, or circular references, which
* will cause objects containing such values to not be handled correctly.
*/
class UniquePropertySet {
constructor() {
this.map = new Map();
}
add(obj) {
const key = this._serialize(obj);
if (!this.map.has(key)) {
this.map.set(key, obj);
}
}
has(obj) {
const key = this._serialize(obj);
return this.map.has(key);
}
delete(obj) {
const key = this._serialize(obj);
return this.map.delete(key);
}
_serialize(obj) {
// Sort the object's keys to ensure consistent order
return JSON.stringify(obj, Object.keys(obj).sort());
}
clear() {
return this.map.clear();
}
get size() {
return this.map.size;
}
values() {
return this.map.values();
}
*[Symbol.iterator]() {
for (let value of this.map.values()) yield value;
}
}
// Usage
const assert = require("assert");
const uniqueObjects = new UniquePropertySet();
uniqueObjects.add({ a: 1, b: 2 });
uniqueObjects.add({ b: 2, a: 1 }); // Won't be added, as it's considered a duplicate
assert.strictEqual(uniqueObjects.size, 1);
uniqueObjects.add({ a: 1, b: 2, c: 3 }); // Will be added, as it's a different object
assert.strictEqual(uniqueObjects.size, 2);
uniqueObjects.delete({ a: 1, b: 2 }); // Correctly deletes the first entry
assert.strictEqual(uniqueObjects.size, 1);
// Nested objects are supported too
uniqueObjects.clear();
uniqueObjects.add({ a: 1, b: { c: 2 } });
uniqueObjects.add({ a: 1, b: { c: 2 } });
assert.strictEqual(uniqueObjects.size, 1);
// Even nested objects are supported
uniqueObjects.clear();
uniqueObjects.add({ a: 1, b: { d: 3, c: 2 } });
uniqueObjects.add({ a: 1, b: { c: 2, d: 3 } });
assert.strictEqual(uniqueObjects.size, 1);
@ganes369
Copy link

ganes369 commented Dec 2, 2023

Opa, iae blz? era isso msm "Daí, se eu entendi bem a sua dúvida aqui, é porque você está curioso se fazer um for..of resolveria, ou seja, se seria mais rápido" eu ainda ñ chegai nesse módulo ainda "benchmark" msm valeuzão pela resposta...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment