I'm using the rangy library and reading the docs for detach: "Destroys the range when it is no longer to be used.".

Currently there's no good place for me to call detach in my code for some ranges I create with cloneRange. Since it's an editor I'm working on this code can be called loads of times.

Should I be worried about memory consumption since I'm not calling detach or will garbage collection finally collect the non-referenced ranges?

Basically this boils down to the question: will Rangy keep references to created ranges with cloneRange preventing garbage collection to work properly?

有帮助吗?

解决方案

There are really two detach methods: the one on native ranges (those ranges created by document.createRange()), and the method that Rangy exposes on its range objects.

DOM's detach

The reason for detach being there at all is that it is mandated by the DOM Level 2.

Now, going by the discussion in this bug report, the idea of detach was that it should enable the browser to free up computational resources. As long as a range is in use, the browser has to track it. Judging by the discussion in the bug report, the following appear to be true:

  1. Few applications call Range.detach.

  2. In Firefox, it is a noop.

Moreover, the WHATWG makes it a noop so it is likely that browsers will follow suit sooner or later.

Rangy's detach

The detach method that Rangy puts on its ranges calls the DOM's method and performs some internal bookkeeping.

Looking at Rangy's code, I see nothing there that would suggest that calling detach on a range created by Rangy would prevent garbage collection problems.

I was wondering what Tim Down (author of Rangy) thought about it, and I found a post by him here. He seems to be on board with getting rid of detach(). Probably it will take a while before Rangy removes it if only to be backward compatible.

So I would not bother calling it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top