memoized 2.2.6 copy "memoized: ^2.2.6" to clipboard
memoized: ^2.2.6 copied to clipboard

Decorators to cache returned values, it helps in reducing the execution time of the function by using LRU Caching.

example/memoized.dart

import 'package:memoized/memoized.dart';
import './timer.dart';

class MemoizedExample {
  late final sumRange = Memoized2((int begin, int end) {
    int sum = 0;
    for (int i = begin; i <= end; i++) {
      sum += i;
    }
    return sum;
  });

  static final one = BigInt.one;
  static final two = BigInt.two;
  late final Memoized1<BigInt, BigInt> fibonacci = Memoized1((n) {
    if (n <= one) return n;
    return fibonacci(n - one) + fibonacci(n - two);
  });
}

void main() {
  final example = MemoizedExample();
  time(() => example.sumRange(0, 999999999));
  time(() => example.sumRange(0, 999999999)); // cached data

  // recursion by using cached data (top-down dynamic programming)
  time(() => example.fibonacci(BigInt.parse('300')));
}
7
likes
140
pub points
86%
popularity

Publisher

verified publishersylfree.com

Decorators to cache returned values, it helps in reducing the execution time of the function by using LRU Caching.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

quiver

More

Packages that depend on memoized