framework_benchmark.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //! JS Framework Benchmark
  2. //! ----------------------
  3. //!
  4. //! This example is used in the JS framework benchmarking tool to compare Dioxus' performance with other frontend frameworks.
  5. //!
  6. //!
  7. //!
  8. use std::rc::Rc;
  9. use dioxus::events::on::MouseEvent;
  10. use dioxus_core as dioxus;
  11. use dioxus_core::prelude::*;
  12. use dioxus_web::WebsysRenderer;
  13. use dioxus_html_namespace as dioxus_elements;
  14. fn main() {
  15. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  16. console_error_panic_hook::set_once();
  17. log::debug!("starting!");
  18. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App));
  19. }
  20. // We use a special immutable hashmap to make hashmap operations efficient
  21. type RowList = im_rc::HashMap<usize, Rc<str>, FxBuildHasher>;
  22. // type RowList = im_rc::HashMap<usize, Rc<str>, nohash_hasher::BuildNoHashHasher<usize>>;
  23. static App: FC<()> = |cx| {
  24. let (items, set_items) = use_state_classic(cx, || RowList::default());
  25. let (selection, set_selection) = use_state_classic(cx, || None as Option<usize>);
  26. let create_rendered_rows = move |from, num| move |_| set_items(create_row_list(from, num));
  27. let append_1_000_rows =
  28. move |_| set_items(create_row_list(items.len(), 1000).union(items.clone()));
  29. let update_every_10th_row = move |_| {
  30. let mut new_items = items.clone();
  31. let mut small_rng = SmallRng::from_entropy();
  32. new_items
  33. .iter_mut()
  34. .step_by(10)
  35. .for_each(|(_, val)| *val = create_new_row_label(&mut small_rng));
  36. set_items(new_items);
  37. };
  38. let clear_rows = move |_| set_items(RowList::default());
  39. let swap_rows = move |_| {
  40. // this looks a bit ugly because we're using a hashmap instead of a vec
  41. if items.len() > 998 {
  42. let mut new_items = items.clone();
  43. let a = new_items.get(&0).unwrap().clone();
  44. *new_items.get_mut(&0).unwrap() = new_items.get(&998).unwrap().clone();
  45. *new_items.get_mut(&998).unwrap() = a;
  46. set_items(new_items);
  47. }
  48. };
  49. let rows = items.iter().map(|(key, value)| {
  50. rsx!(Row {
  51. key: "{key}",
  52. row_id: *key as usize,
  53. label: value.clone(),
  54. })
  55. });
  56. cx.render(rsx! {
  57. div { class: "container"
  58. div { class: "jumbotron"
  59. div { class: "row"
  60. div { class: "col-md-6", h1 { "Dioxus" } }
  61. div { class: "col-md-6"
  62. div { class: "row"
  63. ActionButton { name: "Create 1,000 rows", id: "run", action: create_rendered_rows(0, 1_000) }
  64. ActionButton { name: "Create 10,000 rows", id: "runlots", action: create_rendered_rows(0, 10_000) }
  65. ActionButton { name: "Append 1,000 rows", id: "add", action: append_1_000_rows }
  66. ActionButton { name: "Update every 10th row", id: "update", action: update_every_10th_row, }
  67. ActionButton { name: "Clear", id: "clear", action: clear_rows }
  68. ActionButton { name: "Swap rows", id: "swaprows", action: swap_rows }
  69. }
  70. }
  71. }
  72. }
  73. table {
  74. tbody {
  75. {rows}
  76. }
  77. }
  78. span {}
  79. }
  80. })
  81. };
  82. #[derive(Props)]
  83. struct ActionButtonProps<F: Fn(MouseEvent)> {
  84. name: &'static str,
  85. id: &'static str,
  86. action: F,
  87. }
  88. fn ActionButton<F: Fn(MouseEvent)>(cx: Context<ActionButtonProps<F>>) -> VNode {
  89. cx.render(rsx! {
  90. div { class: "col-sm-6 smallpad"
  91. button {class:"btn btn-primary btn-block", r#type: "button", id: "{cx.id}", onclick: {&cx.action},
  92. "{cx.name}"
  93. }
  94. }
  95. })
  96. }
  97. #[derive(PartialEq, Props)]
  98. struct RowProps {
  99. row_id: usize,
  100. label: Rc<str>,
  101. }
  102. fn Row<'a>(cx: Context<'a, RowProps>) -> VNode {
  103. cx.render(rsx! {
  104. tr {
  105. td { class:"col-md-1", "{cx.row_id}" }
  106. td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  107. a { class: "lbl", "{cx.label}" }
  108. }
  109. td { class: "col-md-1"
  110. a { class: "remove", onclick: move |_| {/* remove */}
  111. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  112. }
  113. }
  114. td { class: "col-md-6" }
  115. }
  116. })
  117. }
  118. use fxhash::{FxBuildHasher, FxHasher32};
  119. use rand::prelude::*;
  120. fn create_new_row_label(rng: &mut SmallRng) -> Rc<str> {
  121. let mut label = String::new();
  122. label.push_str(ADJECTIVES.choose(rng).unwrap());
  123. label.push(' ');
  124. label.push_str(COLOURS.choose(rng).unwrap());
  125. label.push(' ');
  126. label.push_str(NOUNS.choose(rng).unwrap());
  127. Rc::from(label)
  128. }
  129. fn create_row_list(from: usize, num: usize) -> RowList {
  130. let mut small_rng = SmallRng::from_entropy();
  131. (from..num + from)
  132. .map(|f| (f, create_new_row_label(&mut small_rng)))
  133. .collect::<RowList>()
  134. }
  135. static ADJECTIVES: &[&str] = &[
  136. "pretty",
  137. "large",
  138. "big",
  139. "small",
  140. "tall",
  141. "short",
  142. "long",
  143. "handsome",
  144. "plain",
  145. "quaint",
  146. "clean",
  147. "elegant",
  148. "easy",
  149. "angry",
  150. "crazy",
  151. "helpful",
  152. "mushy",
  153. "odd",
  154. "unsightly",
  155. "adorable",
  156. "important",
  157. "inexpensive",
  158. "cheap",
  159. "expensive",
  160. "fancy",
  161. ];
  162. static COLOURS: &[&str] = &[
  163. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  164. "orange",
  165. ];
  166. static NOUNS: &[&str] = &[
  167. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  168. "pizza", "mouse", "keyboard",
  169. ];