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')));
}
11
likes
160
points
923
downloads

Publisher

verified publishersylfree.com

Weekly Downloads

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

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

quiver

More

Packages that depend on memoized