rows.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #![allow(non_snake_case, non_upper_case_globals)]
  2. //! This benchmark tests just the overhead of Dioxus itself.
  3. //!
  4. //! For the JS Framework Benchmark, both the framework and the browser is benchmarked together. Dioxus prepares changes
  5. //! to be made, but the change application phase will be just as performant as the vanilla wasm_bindgen code. In essence,
  6. //! we are measuring the overhead of Dioxus, not the performance of the "apply" phase.
  7. //!
  8. //! On my MBP 2019:
  9. //! - Dioxus takes 3ms to create 1_000 rows
  10. //! - Dioxus takes 30ms to create 10_000 rows
  11. //!
  12. //! As pure "overhead", these are amazing good numbers, mostly slowed down by hitting the global allocator.
  13. //! These numbers don't represent Dioxus with the heuristic engine installed, so I assume it'll be even faster.
  14. use criterion::{criterion_group, criterion_main, Criterion};
  15. use dioxus_core as dioxus;
  16. use dioxus_core::prelude::*;
  17. use dioxus_core_macro::*;
  18. use dioxus_html as dioxus_elements;
  19. use rand::prelude::*;
  20. fn main() {
  21. static App: Component<()> = |cx| {
  22. let mut rng = SmallRng::from_entropy();
  23. let rows = (0..10_000_usize).map(|f| {
  24. let label = Label::new(&mut rng);
  25. rsx! {
  26. Row {
  27. row_id: f,
  28. label: label
  29. }
  30. }
  31. });
  32. cx.render(rsx! {
  33. table {
  34. tbody {
  35. {rows}
  36. }
  37. }
  38. })
  39. };
  40. let mut dom = VirtualDom::new(App);
  41. let g = dom.rebuild();
  42. assert!(g.edits.len() > 1);
  43. }
  44. #[derive(PartialEq, Props)]
  45. struct RowProps {
  46. row_id: usize,
  47. label: Label,
  48. }
  49. fn Row(cx: Scope<RowProps>) -> Element {
  50. let [adj, col, noun] = cx.props.label.0;
  51. cx.render(rsx! {
  52. tr {
  53. td { class:"col-md-1", "{cx.props.row_id}" }
  54. td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  55. a { class: "lbl", "{adj}" "{col}" "{noun}" }
  56. }
  57. td { class: "col-md-1"
  58. a { class: "remove", onclick: move |_| {/* remove */}
  59. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  60. }
  61. }
  62. td { class: "col-md-6" }
  63. }
  64. })
  65. }
  66. #[derive(PartialEq)]
  67. struct Label([&'static str; 3]);
  68. impl Label {
  69. fn new(rng: &mut SmallRng) -> Self {
  70. Label([
  71. ADJECTIVES.choose(rng).unwrap(),
  72. COLOURS.choose(rng).unwrap(),
  73. NOUNS.choose(rng).unwrap(),
  74. ])
  75. }
  76. }
  77. static ADJECTIVES: &[&str] = &[
  78. "pretty",
  79. "large",
  80. "big",
  81. "small",
  82. "tall",
  83. "short",
  84. "long",
  85. "handsome",
  86. "plain",
  87. "quaint",
  88. "clean",
  89. "elegant",
  90. "easy",
  91. "angry",
  92. "crazy",
  93. "helpful",
  94. "mushy",
  95. "odd",
  96. "unsightly",
  97. "adorable",
  98. "important",
  99. "inexpensive",
  100. "cheap",
  101. "expensive",
  102. "fancy",
  103. ];
  104. static COLOURS: &[&str] = &[
  105. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  106. "orange",
  107. ];
  108. static NOUNS: &[&str] = &[
  109. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  110. "pizza", "mouse", "keyboard",
  111. ];