A live collection can *not* be emulated in JS.
We're talking about *browser* methods, and whether the specification
should or shouldn't support live collections. I've already said that
it would be great if it allowed both live and static. The reason I
would like live collections is that for those cases where a static
collection is indicated, it is much simpler in JS to convert a live
collection to static than vice versa (e.g. you have to re-fetch the
static collection each time or update it some other way).
In any case, live collections can be emulated in JS, e.g. you can
provide a DOM manipulation API and specify that collections are only
live if the API is used. You don't have to emulate all the features
of general DOM manipulation API, just the bits required for the job.
At the risk of repeating myself...
I'd prefer
to have live lists and convert them to static using JS than having
static lists that I have to update every time I use them to imitate
live lists. [...]
Unless you provide new information, the I'll reach the same conclusion
(to do otherwise would be illogical). There are dozens of ways to
implement particular functions, 5 or 10 will be equivalent for
criteria such as performance, robustness and maintainability. It is
myopic to assume that there is only one way to do something and naive
to think that that one way will occur to everyone who approaches the
problem independently.
(1) There are only 2 possibilities:
Upon entering the getter, you either
-are confident that the collection is already updated (in that case,
every DOM touch operation must update all the live collections ~=
nonsense),
Not at all. Another method is to keep references to the collections
that an element belongs to so that when the element is modified so is
the collection. Another is to create an index system for the most
common collections, there are likely others.
or,
-it needs to check first wether an update is required, and update or
not, accordingly.
And they always are, much more so if/when the DOM is dirty.
I guess you are basing that on your tests, see comments below.
[...]
See for yourself:
http://jorgechamorro.com/cljs/058/
qSA is available in Safari 3 and 4, Chrome, FF3.5b, Opera 10 and IE8,
AFAIK.
When dirty DOM: ON, the DOM is touched to be sure that it's dirty.
And let me know,
The flaws I see in the test are:
1. It doesn't update the static collection when the DOM is dirty -
clearly that needs to be done if the static collection is to provide
the same functionality as a live one
2. It doesn't test scenarios of looping over a collection when some
part of the DOM that doesn't belong to the collection is modified
3. It gets the length of collections every time - that may be
irrelevant, but it might not either.
I ran the script in Safari 4 on a 1GHz G4 MacBook, Firefox 3 died. The
results were:
Method Dirty not Dirty
(ms) (ms)
live 350 2.61
static 118 0.35
qSA 122 2.49
Summary: static was about 3 times faster than live for dirty, 8 times
faster for clean. qSA is the same speed as static for dirty, but the
same speed as live for clean (dunno what to make of that).
What do I get from the above?
1. Live collections are updated whenever elements in the collection
are modified (as expected). It doesn't show whether it is updated:
a. unconditionally whenever the DOM is modified,
b. only when a member of the collection is modified,
c. only when the collection is accessed, or
d. some other epoch
can't be determined from this test (the suggested non-collection DOM
modification will help clear that up). The test also doesn't reveal
how the collection is updated (is it re-fetched or a more
sophisticated algorithm used).
2. The comparison between qSA and static shows probably nothing
special happens when a static collection is accessed (as expected)
3. Accessing the members of an array by index is faster than accessing
the members of a live collection by index. Why that occurs can't be
determined by the test, it may have nothing to do with the nature of
the collection and more to do with the fact that arrays might be more
highly optimsed for such access. There is no reason why accessing the
collection is slower when the DOM hasn't been modified.
4. Whether a live or static collection is more efficient can only be
determined on a case by case basis.
If the DOM methods don't provide live collections, that's one option
that isn't available. Implementing live collections from static in JS
is likely much more expensive than implementing static from live.
However, the best solution would be for both to be available so
programers have a wider range of tools at their disposal.