jsframework.rs 3.3 KB

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