jsframework.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 rand::prelude::*;
  22. criterion_group!(mbenches, create_rows);
  23. criterion_main!(mbenches);
  24. fn create_rows(c: &mut Criterion) {
  25. fn app(cx: Scope) -> Element {
  26. let mut rng = SmallRng::from_entropy();
  27. render!(
  28. table {
  29. tbody {
  30. (0..10_000_usize).map(|f| {
  31. let label = Label::new(&mut rng);
  32. rsx!( Row { row_id: f, label: label } )
  33. })
  34. }
  35. }
  36. )
  37. }
  38. c.bench_function("create rows", |b| {
  39. let mut dom = VirtualDom::new(app);
  40. dom.rebuild();
  41. b.iter(|| {
  42. let g = dom.rebuild();
  43. assert!(g.dom_edits.len() > 1);
  44. })
  45. });
  46. }
  47. #[derive(PartialEq, Props)]
  48. struct RowProps {
  49. row_id: usize,
  50. label: Label,
  51. }
  52. fn Row(cx: Scope<RowProps>) -> Element {
  53. let [adj, col, noun] = cx.props.label.0;
  54. cx.render(rsx! {
  55. tr {
  56. td { class:"col-md-1", "{cx.props.row_id}" }
  57. td { class:"col-md-1", onclick: move |_| { /* run onselect */ },
  58. a { class: "lbl", "{adj}" "{col}" "{noun}" }
  59. }
  60. td { class: "col-md-1",
  61. a { class: "remove", onclick: move |_| {/* remove */},
  62. span { class: "glyphicon glyphicon-remove remove", aria_hidden: "true" }
  63. }
  64. }
  65. td { class: "col-md-6" }
  66. }
  67. })
  68. }
  69. #[derive(PartialEq)]
  70. struct Label([&'static str; 3]);
  71. impl Label {
  72. fn new(rng: &mut SmallRng) -> Self {
  73. Label([
  74. ADJECTIVES.choose(rng).unwrap(),
  75. COLOURS.choose(rng).unwrap(),
  76. NOUNS.choose(rng).unwrap(),
  77. ])
  78. }
  79. }
  80. static ADJECTIVES: &[&str] = &[
  81. "pretty",
  82. "large",
  83. "big",
  84. "small",
  85. "tall",
  86. "short",
  87. "long",
  88. "handsome",
  89. "plain",
  90. "quaint",
  91. "clean",
  92. "elegant",
  93. "easy",
  94. "angry",
  95. "crazy",
  96. "helpful",
  97. "mushy",
  98. "odd",
  99. "unsightly",
  100. "adorable",
  101. "important",
  102. "inexpensive",
  103. "cheap",
  104. "expensive",
  105. "fancy",
  106. ];
  107. static COLOURS: &[&str] = &[
  108. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  109. "orange",
  110. ];
  111. static NOUNS: &[&str] = &[
  112. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  113. "pizza", "mouse", "keyboard",
  114. ];