DEV Community

Mateo Guzmán
Mateo Guzmán

Posted on

LZ4 – C++ React Native bindings for an extremely fast compression algorithm

I've been dipping my toes into JSI and C++ lately and, as a result, I got to build a small package called react-native-lz4. It’s a library for fast file compression in React Native using the LZ4 algorithm written in C.

It is still experimental as I'm still polishing the error handling and extending its API but it can already be used (with caution!)

Package: https://github.com/mateoguzmana/react-native-lz4
You can learn more about LZ4 on its website: https://lz4.org/

The package supports both old and new architecture, and currently exposes two main functions to compress and decompress any type of file.

Basic example:

import { compressFile, decompressFile } from 'react-native-lz4';

function onProgress(processedSize: number, totalSize: number) {
  // e.g. { processedSize: 50, totalSize: 100, progress: '50%' }
  console.log({
    processedSize,
    totalSize,
    progress: `${Math.round((processedSize / totalSize) * 100)}%`,
  });
}

const compressionResult = await compressFile(
  'path/to/file',
  'path/to/output',
  onProgress
);
const decompressionResult = await decompressFile(
  'path/to/file',
  'path/to/output',
  onProgress
);

console.log(compressionResult);
// { success: true, message: 'File compressed successfully', originalSize: 100, finalSize: 50 }

console.log(decompressionResult);
// { success: true, message: 'File decompressed successfully', originalSize: 50, finalSize: 100 }
Enter fullscreen mode Exit fullscreen mode

Sentry mobile image

App store rankings love fast apps - mobile vitals can help you get there

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read full post →

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay