On 05/04/2016 01:43 PM, /#!/JoePea wrote:
For example, I have some code that uses a Map just to keep a
collection of things (the keys) but values are not important, so they
are undefined, like this:
```js
let something = {}
let otherThing = {}
let m = new Map
m.set(something)
m.set(
otherThing
)
```
where the values for those object keys are `undefined` since there's
not second arg to `m.set`. If I add the line
```js
m.clear()
```
and retain references to `something`, `otherThing`, and `m`, I know
that those objects won't be GCed. What about the `undefined` values?
Are those `undefined` values something that the GC has to collect? Or
do `undefined` values literally reference nothing, not needing to be
collected?
First, use Set instead of Map if a set is what you want.
Second, this is an implementation question, since current GC is
unobservable except for performance and even with WeakRef or something
making it observable, it wouldn't come into play for your example. So
this isn't really the list for it.
But nobody's going to make undefined be a GC thing.
On the other hand, there are language-invisible entries in a hashtable
hiding behind your Map or Set. And those *might* be GC-able. Heck, they
probably will be, since you wouldn't really want to run out of memory if
you repetitively threw stuff into a Set and clear()ed it out over and
over again.
Just wondering because I want to avoid GC while rendering animations
at 60fps. I know I can prevent GC if I retain a value to some
constant, as in
```js
let something = {}
let otherThing = {}
const foo = true
let m = new Map
m.set(something, foo)
m.set(
otherThing , foo
)
m.clear()
```
so then if I retain the reference to `foo` then there's no GC; I'm
just sticking things in and out of the Map, but I'm curious to know
how `undefined` is treated, because if that prevents GC, then the code
can be cleaner.
In practical terms, the behavior with undefined is not going to have any
more GCs. But are you sure the above is completely GC-safe?
For example, in SpiderMonkey, the below GCs 30 times when run from the
shell:
var o1 = {};
var o2 = {};
var s = new Set();
for (i = 0; i < 10000000; i++) {
s.add(o1);
s.add(o2);
s.clear();
}
If I remove the s.add lines, it will GC 2 times (there are 2 forced
shutdown GCs).
The only v8 shell I have lying around is too old (3.14.5.10) to have
Set, so I can't tell you what it would do.
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss