estimateSize static method

int estimateSize(
  1. Object? value, {
  2. int maxDepth = 5,
})

Implementation

static int estimateSize(Object? value, {int maxDepth = 5}) {
  maxDepth--;
  if (maxDepth < 0) {
    throw StateError('Reached maximum depth');
  }
  if (value is Uint8List) {
    return value.lengthInBytes;
  }
  if (value is String) {
    // Length is in UTF-16 code units.
    // Therefore, we multiply by 2.
    return 2 * value.length;
  }
  if (value is Iterable) {
    var sum = 0;
    for (var item in value) {
      sum += estimateSize(item, maxDepth: maxDepth);
    }
    return 64 + sum + value.length * 8;
  }
  if (value is Map) {
    var sum = 0;
    for (var item in value.keys) {
      sum += estimateSize(item, maxDepth: maxDepth);
    }
    for (var item in value.values) {
      sum += estimateSize(item, maxDepth: maxDepth);
    }
    return 64 + sum + value.length * 16;
  }
  return 8;
}