Dart: static vs dynamic resource utilization Dart: static vs dynamic resource utilization dart dart

Dart: static vs dynamic resource utilization


You are right, it is definitely faster to load an existing button rather than create one. Parsing markup is highly optimized in browsers, plus having to create and add the element to the DOM is extra overhead, as you mentioned. I ran a simple benchmark with the Stopwatch class:

ButtonElement button1 = querySelector("#button") as ButtonElement    ..name = "fizz"    ..onClick.listen(handle);Stopwatch s = new Stopwatch()    ..start();for(int i = 0; i < 1000; i++) {  ButtonElement button1 = querySelector("#button") as ButtonElement      ..name = "fizz$i"      ..onClick.listen(handle);}s.stop();print('queried button: ${s.elapsedMilliseconds / 1000} ms');ButtonElement button2 = new ButtonElement()    ..name = "fizz2"    ..onClick.listen(handle);document.body.children.add(button2);  s.start();for(int i = 0; i < 1000; i++) {  ButtonElement button2 = new ButtonElement()      ..name = "fizz2$i"      ..onClick.listen(handle);  document.body.children.add(button2);  }s.stop();print('created button: ${s.elapsedMilliseconds / 1000} ms');

Results: queried button: 0.01 ms, created button: 0.022 ms.