js_bench.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. use std::cell::Cell;
  2. use dioxus::prelude::*;
  3. use dioxus_core as dioxus;
  4. use dioxus_core_macro::*;
  5. use dioxus_hooks::{use_ref, use_state};
  6. use dioxus_html as dioxus_elements;
  7. use dioxus_web;
  8. use rand::prelude::*;
  9. fn main() {
  10. console_error_panic_hook::set_once();
  11. if cfg!(debug_assertions) {
  12. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  13. log::debug!("hello world");
  14. }
  15. for a in ADJECTIVES {
  16. wasm_bindgen::intern(*a);
  17. }
  18. for a in COLOURS {
  19. wasm_bindgen::intern(*a);
  20. }
  21. for a in NOUNS {
  22. wasm_bindgen::intern(*a);
  23. }
  24. for a in [
  25. "container",
  26. "jumbotron",
  27. "row",
  28. "Dioxus",
  29. "col-md-6",
  30. "col-md-1",
  31. "Create 1,000 rows",
  32. "run",
  33. "Create 10,000 rows",
  34. "runlots",
  35. "Append 1,000 rows",
  36. "add",
  37. "Update every 10th row",
  38. "update",
  39. "Clear",
  40. "clear",
  41. "Swap rows",
  42. "swaprows",
  43. "preloadicon glyphicon glyphicon-remove", //
  44. "aria-hidden",
  45. "onclick",
  46. "true",
  47. "false",
  48. "danger",
  49. "type",
  50. "id",
  51. "class",
  52. "glyphicon glyphicon-remove remove",
  53. "dioxus-id",
  54. "dioxus-event-click",
  55. "dioxus",
  56. "click",
  57. "1.10",
  58. "lbl",
  59. "remove",
  60. "dioxus-event",
  61. "col-sm-6 smallpad",
  62. "btn btn-primary btn-block",
  63. "",
  64. " ",
  65. ] {
  66. wasm_bindgen::intern(a);
  67. }
  68. for x in 0..100_000 {
  69. wasm_bindgen::intern(&x.to_string());
  70. }
  71. dioxus_web::launch(App);
  72. }
  73. #[derive(Clone, PartialEq, Copy)]
  74. struct Label {
  75. key: usize,
  76. labels: [&'static str; 3],
  77. }
  78. static mut Counter: Cell<usize> = Cell::new(1);
  79. impl Label {
  80. fn new_list(num: usize) -> Vec<Self> {
  81. let mut rng = SmallRng::from_entropy();
  82. let mut labels = Vec::with_capacity(num);
  83. let offset = unsafe { Counter.get() };
  84. unsafe { Counter.set(offset + num) };
  85. for k in offset..(offset + num) {
  86. labels.push(Label {
  87. key: k,
  88. labels: [
  89. ADJECTIVES.choose(&mut rng).unwrap(),
  90. COLOURS.choose(&mut rng).unwrap(),
  91. NOUNS.choose(&mut rng).unwrap(),
  92. ],
  93. });
  94. }
  95. labels
  96. }
  97. }
  98. static App: Component = |cx| {
  99. let mut items = use_ref(&cx, || vec![]);
  100. let mut selected = use_state(&cx, || None);
  101. cx.render(rsx! {
  102. div { class: "container"
  103. div { class: "jumbotron"
  104. div { class: "row"
  105. div { class: "col-md-6", h1 { "Dioxus" } }
  106. div { class: "col-md-6"
  107. div { class: "row"
  108. ActionButton { name: "Create 1,000 rows", id: "run",
  109. onclick: move || items.set(Label::new_list(1_000)),
  110. }
  111. ActionButton { name: "Create 10,000 rows", id: "runlots",
  112. onclick: move || items.set(Label::new_list(10_000)),
  113. }
  114. ActionButton { name: "Append 1,000 rows", id: "add",
  115. onclick: move || items.write().extend(Label::new_list(1_000)),
  116. }
  117. ActionButton { name: "Update every 10th row", id: "update",
  118. onclick: move || items.write().iter_mut().step_by(10).for_each(|item| item.labels[2] = "!!!"),
  119. }
  120. ActionButton { name: "Clear", id: "clear",
  121. onclick: move || items.write().clear(),
  122. }
  123. ActionButton { name: "Swap Rows", id: "swaprows",
  124. onclick: move || items.write().swap(0, 998),
  125. }
  126. }
  127. }
  128. }
  129. }
  130. table { class: "table table-hover table-striped test-data"
  131. tbody { id: "tbody"
  132. {items.read().iter().enumerate().map(|(id, item)| {
  133. let [adj, col, noun] = item.labels;
  134. let is_in_danger = if (*selected).map(|s| s == id).unwrap_or(false) {"danger"} else {""};
  135. rsx!(tr {
  136. class: "{is_in_danger}",
  137. key: "{id}",
  138. td { class:"col-md-1" }
  139. td { class:"col-md-1", "{item.key}" }
  140. td { class:"col-md-1", onclick: move |_| selected.set(Some(id)),
  141. a { class: "lbl", "{adj} {col} {noun}" }
  142. }
  143. td { class: "col-md-1"
  144. a { class: "remove", onclick: move |_| { items.write().remove(id); },
  145. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  146. }
  147. }
  148. td { class: "col-md-6" }
  149. })
  150. })}
  151. }
  152. }
  153. span { class: "preloadicon glyphicon glyphicon-remove" aria_hidden: "true" }
  154. }
  155. })
  156. };
  157. #[derive(Props)]
  158. struct ActionButtonProps<'a> {
  159. name: &'static str,
  160. id: &'static str,
  161. onclick: &'a dyn Fn(),
  162. }
  163. fn ActionButton<'a>(cx: Scope<'a, ActionButtonProps<'a>>) -> Element {
  164. rsx!(cx, div { class: "col-sm-6 smallpad"
  165. button { class:"btn btn-primary btn-block", r#type: "button", id: "{cx.props.id}", onclick: move |_| (cx.props.onclick)(),
  166. "{cx.props.name}"
  167. }
  168. })
  169. }
  170. static ADJECTIVES: &[&str] = &[
  171. "pretty",
  172. "large",
  173. "big",
  174. "small",
  175. "tall",
  176. "short",
  177. "long",
  178. "handsome",
  179. "plain",
  180. "quaint",
  181. "clean",
  182. "elegant",
  183. "easy",
  184. "angry",
  185. "crazy",
  186. "helpful",
  187. "mushy",
  188. "odd",
  189. "unsightly",
  190. "adorable",
  191. "important",
  192. "inexpensive",
  193. "cheap",
  194. "expensive",
  195. "fancy",
  196. ];
  197. static COLOURS: &[&str] = &[
  198. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  199. "orange",
  200. ];
  201. static NOUNS: &[&str] = &[
  202. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  203. "pizza", "mouse", "keyboard",
  204. ];