0

I implemented a class that can be used to instantiate objects passed to a visitor function that detects duplicated keys in a JSON document. The problem I am trying to solve is detecting duplicate keys that are in a JSON document. More specifically, I want to detect duplicates at the same nesting level, which I will refer to as context from now on.

The class I implemented has 2 fields:

  • stack: an array that stores all visited keys grouped by context (each item in the array holds keys for a given context)
  • visited: which holds the visited keys for the current context (top of the stack)

My question is the following:

Do I need to clear the Set from the references they might holds to the keys when I pop them out of the stack? In other words, is the discarded?.clear() call necessary as the set holds string references to the visited keys?

I understand that the set reference itself will be lost but I am wondering about the references that the set holds (to the visited keys in the json document)

class DetectDuplicates {
  stack;
  visited;

  constructor() {
    this.stack = [];
    this.visited = new Set();
  }

  onObjectBegin() {
    this.stack.push(new Set());
    this.visitedKeys = this.stack[this.stack.length - 1];
  }

  onObjectEnd() {
    const discarded = this.stack.pop();
    // Is that necessary?
    discarded?.clear();
    this.visitedKeys = this.stack[this.stack.length - 1];
  }

}
Monad
  • 85
  • 3
  • This isn't JavaScript, it doesn't have type declarations. – Barmar Jan 19 '22 at 20:22
  • What is `this.scopes`? – Barmar Jan 19 '22 at 20:25
  • When you pop the set out of the array, if there are no other references to the set, it will become garbage. You don't need to clear it. – Barmar Jan 19 '22 at 20:26
  • Elements in a set don't have a reference to the set. – Barmar Jan 19 '22 at 20:27
  • Indeed it's a TS project and I messed up while cleaning it up to remove the complexity let me see clean it up – Monad Jan 19 '22 at 20:33
  • Hopefully it's a bit more explanatory and clearer now – Monad Jan 19 '22 at 20:37
  • You still have `this.scopes.length` and `this.scopes.pop()`. What is `this.scopes`? – Barmar Jan 19 '22 at 20:42
  • you are right, sorry about that, edited again, 'scopes === stack' it's the name I used internally and forget to rename all occurences – Monad Jan 19 '22 at 20:46
  • Not sure what this has to do with class fields. [They are just normal properties](https://stackoverflow.com/a/54851218/1048572), and behave as such for the purposes of garbage collection. – Bergi Jan 19 '22 at 20:57
  • Indeed, it doesn't have to do with class fields, it just happen that's what I am using here, but that's not the relevant part of my question. Should I reword it as does Set holds memory references to string which is more the relevant part of my question – Monad Jan 19 '22 at 21:08

1 Answers1

0

Do I need to clear the Set from the references they might holds to the keys when I pop them out of the stack?

No. If the Set instance is not referenced from anywhere else, it will get garbage-collected, and will take with it all the references stored within it.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375