shouldCompress method

bool shouldCompress(
  1. String data
)

Determines if compression should be applied to the given data.

Returns true if the data should be compressed, false otherwise.

The decision is based on the size of the data and the potential compression ratio. Small data (less than 100 bytes) is not compressed because the overhead of compression would likely outweigh the benefits.

Implementation

bool shouldCompress(String data) {
  // Don't compress small data
  if (data.length < 100) {
    return false;
  }

  // For larger data, estimate the compression ratio
  if (data.length >= 1000) {
    // For very large data, compression is almost always beneficial
    return true;
  }

  // For medium-sized data, do a quick check to estimate compressibility
  // by looking at the entropy of a sample of the data
  final sample = data.length > 500 ? data.substring(0, 500) : data;
  final entropy = _calculateEntropy(sample);

  // If the entropy is low, the data is likely to be compressible
  // Entropy ranges from 0 (completely predictable) to 8 (completely random)
  return entropy < 6.0;
}