Skip to main content

microbench 0.3.0 copy "microbench: ^0.3.0" to clipboard
microbench: ^0.3.0 copied to clipboard

Zero-dependency microbenchmark harness for Dart: robust timing, comparison tables in Markdown, HTML or text, JSON output, and README embedding.

example/example.dart

// Copyright (c) 2026, Sudipto Chandra
// All rights reserved. Check LICENSE file for details.

import 'package:microbench/suite.dart';

/// Joins [count] short strings with `+`.
class ConcatBenchmark extends SyncBenchmark {
  ConcatBenchmark(this.count) : super('+ operator', count);

  final int count;

  @override
  Object? run() {
    var s = '';
    for (var i = 0; i < count; i++) {
      s += 'x';
    }
    return s;
  }
}

String buffer(int n) {
  final sb = StringBuffer();
  for (var i = 0; i < n; i++) {
    sb.write('x');
  }
  return sb.toString();
}

// Try: dart run example/example.dart --thorough --verify --format=md
Future<void> main(List<String> args) => runSuite(
      args.isEmpty ? ['--quick', '--verify'] : args,
      () async {
        // 1. Declared like tests. Declared benchmarks run when this body
        //    ends, compared with the first one in their group.
        group('Repeat one char, 1000 times', () {
          benchmark('+ operator', ConcatBenchmark(1000).run);
          benchmark('StringBuffer', () => buffer(1000));
          benchmark('* operator', () => 'x' * 1000);
        });

        // 2. Every variant on every input, measured right away: one column
        //    per case. With --verify, rows must return equal results.
        await matrix<int>(
          'Building a string',
          cases: [10, 1000],
          label: (n) => '$n chars',
          size: (n) => n,
          metric: Metric.perSecond('chars'),
          variants: {
            '+ operator': (n) => ConcatBenchmark(n).run(),
            'StringBuffer': buffer,
            '* operator': (n) => 'x' * n,
          },
        );

        // 3. A benchmark class on its own, for setup and teardown.
        await ConcatBenchmark(1000).measureRate(
          options: BenchmarkSettings.current.options ?? BenchmarkOptions.quick,
          metric: Metric.perSecond('chars'),
        );
      },
    );
1
likes
0
points
0
downloads

Publisher

verified publisherbitanon.dev

Weekly Downloads

Zero-dependency microbenchmark harness for Dart: robust timing, comparison tables in Markdown, HTML or text, JSON output, and README embedding.

Repository (GitHub)
View/report issues

Topics

#benchmark #performance #profiling #testing

Funding

Consider supporting this project:

paypal.me

License

unknown (license)

More

Packages that depend on microbench