JSFiddle - React, Tailwind, and code Playground

HTML

<script src="https://cdn.jsdelivr.net/npm/lz4-asm@0.4.2/dist/lz4wasm.js"></script>

JavaScript

const lz4module = lz4init();
lz4module.then((lz4) => {
  const lz4js = lz4.lz4js;
  const textEncoder = new TextEncoder();

  const small_value = "abcd";
  const small_value_b = textEncoder.encode(small_value);
  const large_value_b = textEncoder.encode(small_value.repeat(1000));
  const large_random_b = new Uint8Array([...Array(4000).keys()].map((x) => Math.floor(Math.random() * 256)));

  function size_log(value, func) {
    const compressed = (func || lz4js.compress)(value);
    return `${value.length} → ${compressed.length}`;
  }
  console.log("4 letters:", size_log(small_value_b));
  console.log("4000 repeated letters:", size_log(large_value_b));
  console.log("4000 random bytes:", size_log(large_random_b));

  function lz4ext_compress(value) {
		const compressed = lz4js.compress(value);
    if (compressed.length > value.length) {
    	return new Uint8Array([123, ...value]);
    }
    return new Uint8Array([231, ...compressed]);
  }
  function lz4ext_decompress(value) {
  	if (value[0] === 123) {
    	return value.slice(1);
    }
    if (value[0] === 231) {
    	return lz4js.decompress(value.slice(1));
    }
    throw ["unexpected value", value];
  }
  console.log("ext, 4 letters:", size_log(small_value_b, lz4ext_compress));
  console.log("ext, 4000 repeated letters:", size_log(large_value_b, lz4ext_compress));
  console.log("ext, 4000 random bytes:", size_log(large_random_b, lz4ext_compress));
  function arrEqual(first, second) {
    return first.length === second.length && first.every((value, index) => value === second[index]);
  }
  function roundtrip_check_ext(value) {
  	return arrEqual(lz4ext_decompress(lz4ext_compress(value)), value);
  }
	console.log("ext roundtrip:", roundtrip_check_ext(small_value_b), roundtrip_check_ext(large_value_b), roundtrip_check_ext(large_random_b));
})