1
0

jsframework.rs 3.2 KB

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