In Dart, when you run cross-compiled JavaScript, there are two ways of creating an instance of an element, such as a ButtonElement
:
Dynamically (creating a new button and adding it to the browser DOM):
ButtonElement button = new ButtonElement();
button
..name="fizz"
..id="blah"
..onClick.listen(handle);
window.document.body.children.add(button);
Statically (loading an existing button that already exists in the browser DOM):
ButtonElement button = querySelector("#button") as ButtonElement;
button
..name="fizz"
..id="blah"
..onClick.listen(handle);
I'm interested in both speed and memory considerations between the two methods above:
- Which is faster, and why?
- Which has a smaller memory footprint, and why?
My guess is that the former method is slower because it forces JavaScript to create the ButtonElement
(which eats up CPU cycles), which must then be added to the browser DOM.
But I have absolutely no idea which method is more efficient w.r.t. memory, or even how to test for such a thing.