1
0

jsframework.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. //!
  9. //! Pre-templates (Mac M1):
  10. //! - 3ms to create 1_000 rows
  11. //! - 30ms to create 10_000 rows
  12. //!
  13. //! Post-templates
  14. //! - 580us to create 1_000 rows
  15. //! - 6.2ms to create 10_000 rows
  16. //!
  17. //! As pure "overhead", these are amazing good numbers, mostly slowed down by hitting the global allocator.
  18. //! These numbers don't represent Dioxus with the heuristic engine installed, so I assume it'll be even faster.
  19. use criterion::{criterion_group, criterion_main, Criterion};
  20. use dioxus::prelude::*;
  21. use dioxus_core::NoOpMutations;
  22. use rand::prelude::*;
  23. criterion_group!(mbenches, create_rows);
  24. criterion_main!(mbenches);
  25. fn create_rows(c: &mut Criterion) {
  26. c.bench_function("create rows", |b| {
  27. let mut dom = VirtualDom::new(app);
  28. dom.rebuild(&mut dioxus_core::NoOpMutations);
  29. b.iter(|| {
  30. dom.rebuild(&mut NoOpMutations);
  31. })
  32. });
  33. }
  34. fn app() -> Element {
  35. let mut rng = SmallRng::from_entropy();
  36. rsx! (
  37. table {
  38. tbody {
  39. for f in 0..10_000_usize {
  40. table_row {
  41. row_id: f,
  42. label: Label::new(&mut rng)
  43. }
  44. }
  45. }
  46. }
  47. )
  48. }
  49. #[derive(PartialEq, Props, Clone, Copy)]
  50. struct RowProps {
  51. row_id: usize,
  52. label: Label,
  53. }
  54. fn table_row(props: RowProps) -> Element {
  55. let [adj, col, noun] = props.label.0;
  56. rsx! {
  57. tr {
  58. td { class:"col-md-1", "{props.row_id}" }
  59. td { class:"col-md-1", onclick: move |_| { /* run onselect */ },
  60. a { class: "lbl", "{adj}" "{col}" "{noun}" }
  61. }
  62. td { class: "col-md-1",
  63. a { class: "remove", onclick: move |_| {/* remove */},
  64. span { class: "glyphicon glyphicon-remove remove", aria_hidden: "true" }
  65. }
  66. }
  67. td { class: "col-md-6" }
  68. }
  69. }
  70. }
  71. #[derive(PartialEq, Clone, Copy)]
  72. struct Label([&'static str; 3]);
  73. impl Label {
  74. fn new(rng: &mut SmallRng) -> Self {
  75. Label([
  76. ADJECTIVES.choose(rng).unwrap(),
  77. COLOURS.choose(rng).unwrap(),
  78. NOUNS.choose(rng).unwrap(),
  79. ])
  80. }
  81. }
  82. static ADJECTIVES: &[&str] = &[
  83. "pretty",
  84. "large",
  85. "big",
  86. "small",
  87. "tall",
  88. "short",
  89. "long",
  90. "handsome",
  91. "plain",
  92. "quaint",
  93. "clean",
  94. "elegant",
  95. "easy",
  96. "angry",
  97. "crazy",
  98. "helpful",
  99. "mushy",
  100. "odd",
  101. "unsightly",
  102. "adorable",
  103. "important",
  104. "inexpensive",
  105. "cheap",
  106. "expensive",
  107. "fancy",
  108. ];
  109. static COLOURS: &[&str] = &[
  110. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  111. "orange",
  112. ];
  113. static NOUNS: &[&str] = &[
  114. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  115. "pizza", "mouse", "keyboard",
  116. ];