https://github.com/vincevargadev/compute_dart
Flutter's compute function made available for all non-Flutter Dart programs
https://github.com/vincevargadev/compute_dart
concurrent dart isolate
Last synced: 2 months ago
JSON representation
Flutter's compute function made available for all non-Flutter Dart programs
- Host: GitHub
- URL: https://github.com/vincevargadev/compute_dart
- Owner: dartsidedev
- License: bsd-3-clause
- Created: 2022-03-26T14:42:43.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-14T13:21:31.000Z (almost 3 years ago)
- Last Synced: 2023-09-21T05:17:56.026Z (over 1 year ago)
- Topics: concurrent, dart, isolate
- Language: Dart
- Homepage: https://pub.dev/packages/compute
- Size: 19.5 KB
- Stars: 10
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `compute`
> Flutter's compute function made available for all non-Flutter Dart programs
The `compute` package takes Flutter's `compute` function and makes it available for all Dart programs.
[](https://github.com/dartsidedev/compute/actions) [](https://pub.dev/packages/compute 'See compute package info on pub.dev') [](https://pub.dev/publishers/dartside.dev/packages) [](https://github.com/dartsidedev/compute 'Star me on GitHub!')
## Important links
* [Read the source code and **star the repo** on GitHub](https://github.com/dartsidedev/compute)
* [Open an issue on GitHub](https://github.com/dartsidedev/compute/issues)
* [See package on pub.dev](https://pub.dev/packages/compute)
* [Read the docs on pub.dev](https://pub.dev/documentation/compute/latest/)
* [Flutter `foundation`'s `compute` function](https://api.flutter.dev/flutter/foundation/compute-constant.html)**If you enjoy using this package, a thumbs-up on [pub.dev](https://pub.dev/packages/compute) would be highly appreciated! 👍💙**
## Motivation
When working with isolates, wiring up the `SendPort`s, the `ReceivePort`s correctly
is a lot of boilerplate code when all you want to do is spawn an isolate, compute something, and use the computed value.Flutter's `compute` function is a very useful abstraction over isolates that can be useful in all kinds of Dart apps.
Unfortunately, Flutter's `compute` function is not available for a Dart package that doesn't use Flutter,
for example command-line and server-side applications.This package addresses this issue. It extracts the `compute` function from Flutter and makes it available
for all Dart projects, so if you wish to perform some computation on a separate isolate and use its return value, now you can!## Disclaimers
Do not assume that using `compute` will automatically speed up your code:
you should benchmark your Dart applications *with and without the `compute` function*,
and only switch to using `compute` if it really speeds up your application.Keep in mind, that by using `compute`, you lose some flexibility that working directly with isolates would enable you.
The package is safe to be used on web, but there will be no real isolates spawned.
Changes are synced with Flutter's stable branch only (and they are currently synced manually).
## Usage
### Dart
This package works everywhere and doesn't have any Flutter-specific dependency.
```dart
import 'package:compute/compute.dart';int square(int a) => a * a;
Future main() async {
final squared = await compute(square, 5);
print('5^2=$squared');
}
```### Flutter
If you are on a Flutter project, you don't need this package.
This package should only be used in environments where you cannot use Flutter's `compute` function.For your Flutter project, [use the `compute` function directly from Flutter's `foundation`](https://api.flutter.dev/flutter/foundation/compute-constant.html).