Search packages, repositories and app categories

Package detailsv3.0.0

memoize

Returns cached result of function call when inputs were not changed from previous invocation.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
78K
Likes
33
Pub points
130

Install package

flutter pub add memoize

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The memoize package is a Dart utility that caches the results of function calls based on their input parameters. When a function is invoked with the same arguments as a previous call, it returns the cached result instead of recomputing it, improving performance for expensive or frequently called functions. It supports all major platforms including Android, iOS, Web, macOS, Windows, and Linux. The package is particularly useful in reactive or stateful applications where repeated computations can be avoided. It integrates seamlessly into Flutter projects via pub.dev.

Use cases

  • Caching expensive computations
  • Optimizing UI rendering with stable inputs
  • Improving performance in state management
  • Reducing redundant API calls
  • Enhancing responsiveness in data-heavy apps
  • Avoiding repeated calculations in animations

Key features

  • Automatic caching based on input equality
  • Support for async and sync functions
  • Cross-platform compatibility
  • Lightweight and easy to integrate
  • Built-in support for complex types via custom comparators
  • Minimal boilerplate required

Suitable for

  • Developers optimizing performance in Flutter apps
  • State management with frequent recalculations
  • Apps with heavy computational logic
  • Reactive UIs requiring fast updates
  • Teams using functional programming patterns

Considerations

  • Memory usage increases with cache size
  • Cache invalidation must be handled manually if needed
  • Not suitable for functions with side effects
  • May not work well with mutable input objects without custom equality
  • Performance gain depends on function cost and invocation frequency

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Memoize

memoize returns cached result of function call when inputs were not changed from previous invocation.

Warning

memoize_dart now supports dart 2.12.0 and null safety since 3.0.0 version.

For 2.0.0 we drop func as dependency. Because it archived and doesn't support dart2.

If you need dart-1.24.3 use 1.4.0 version

Examples

memo1, memo2, ... memo10 compare arguments with == operator:

import 'dart:math';
import 'package:memoize/memoize.dart';

void main() {
  var rect1 = new Rectangle<int>(0, 0, 10, 10);
  var rect2 = new Rectangle<int>(0, 0, 10, 10);

  var func = memo1((Rectangle<int> a) => a.width * a.height);

  print(func(rect1));
  print(func(rect2)); // cached result is returned
}

imemo1, imemo2, ... imemo10 compare arguments with identical function:

import 'dart:math';
import 'package:memoize/memoize.dart';

void main() {
  var rect1 = new Rectangle<int>(0, 0, 10, 10);
  var rect2 = new Rectangle<int>(0, 0, 10, 10);

  var func = imemo1((Rectangle<int> a) => a.width * a.height);

  print(func(rect1));
  print(func(rect1)); // cached - same object instance
  print(func(rect2)); // not cached - different object instance
}

memo0 caches result on first call and can be used as lazy cache:

import 'dart:math';
import 'package:memoize/memoize.dart';

void main() {
  var func = memo0(() => new Rectangle<int>(0, 0, 10, 10));

  print(func());
  print(func()); // cached - same object instance
}

Contributors

Categories

Source repository

Projects using this package