Jonatas

add benchmarks from value notifier vs get value

  1 +import 'dart:async';
  2 +
  3 +import 'package:flutter/foundation.dart';
  4 +import 'package:get/state_manager.dart';
  5 +
  6 +int times = 3;
  7 +int get last => times - 1;
  8 +
  9 +Future<String> valueNotifier() {
  10 + final c = Completer<String>();
  11 + final value = ValueNotifier<int>(0);
  12 + final timer = Stopwatch();
  13 + timer.start();
  14 +
  15 + value.addListener(() {
  16 + if (last == value.value) {
  17 + timer.stop();
  18 + c.complete("""${value.value} item value notifier
  19 +objs time: ${timer.elapsedMicroseconds}ms""");
  20 + }
  21 + });
  22 +
  23 + for (var i = 0; i < times; i++) {
  24 + value.value = i;
  25 + }
  26 +
  27 + return c.future;
  28 +}
  29 +
  30 +Future<String> getValue() {
  31 + final c = Completer<String>();
  32 + final value = Value<int>(0);
  33 + final timer = Stopwatch();
  34 + timer.start();
  35 +
  36 + value.addListener(() {
  37 + if (last == value.value) {
  38 + timer.stop();
  39 + c.complete("""${value.value} item get value objs
  40 + time: ${timer.elapsedMicroseconds}ms""");
  41 + }
  42 + });
  43 +
  44 + for (var i = 0; i < times; i++) {
  45 + value.value = i;
  46 + }
  47 +
  48 + return c.future;
  49 +}
  50 +
  51 +Future<String> getStream() {
  52 + final c = Completer<String>();
  53 +
  54 + final value = StreamController<int>();
  55 + final timer = Stopwatch();
  56 + timer.start();
  57 +
  58 + value.stream.listen((v) {
  59 + if (last == v) {
  60 + timer.stop();
  61 + c.complete("$v item stream objs time: ${timer.elapsedMicroseconds}ms");
  62 + }
  63 + });
  64 +
  65 + for (var i = 0; i < times; i++) {
  66 + value.add(i);
  67 + }
  68 +
  69 + return c.future;
  70 +}
  71 +
  72 +void main() async {
  73 + print(await getValue());
  74 + print(await valueNotifier());
  75 + print(await getStream());
  76 + times = 30000;
  77 + print(await getValue());
  78 + print(await valueNotifier());
  79 + print(await getStream());
  80 +}
  81 +
  82 +typedef VoidCallback = void Function();