
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
async-lz-string
Advanced tools
This is a JavaScript library which compresses and decompresses strings. It implements the same algorithm as the lz-string library but unlike that library this one is async and leaves time for other tasks to run while compressing/decompressing.
The reason for complicating the original lz-string library with async is that on slow devices it can take several seconds to compress long strings. If this wasn't done async the page would not be responsive to click events or anything during this time. So if compressing longer strings without causing the page to freeze is desired, async is the best option. If freezing the page is not a problem, the original lz-string will likely have better performance and be simpler to use since it does not require using promises.
The asynchronicity is implemented by awaiting a timeout of 0 after a certain number of iterations. This will let the browser run event handlers before the next iteration. The number of iterations between waits is tuned to be a number which doesn't affect the time taken to compress too much while still being frequent enough to not cause significant delay in processing events.
Simply put: This project makes strings smaller. Why is that useful?
The first thing that comes to mind is localStorage
. localStorage
has limited space on many platforms, so if you for some reason need to store big strings in there this library can help you keep your strings small.
Note that the compression can be combined with JSON.stringify
to compress any object that can be serialized to JSON.
The second thing that comes to mind is transfering things to the server. People on slow internet or with tight bandwidth caps will probably be happier if you transfer smaller amounts of data. Servers often GZip their responses but requests are usually not compressed in any way. If your requests are large enough it may be worth compressing them first, though it's not recommended that you always do it since it will make your code messier.
For a list of server side libraries implementing the same algorithm, see this page.
Add async-lz-string
to your package.json and install it.
There are four functions exported as a UMD module: compressToUTF16, decompressFromUTF16, compressToBase64, and decompressFromBase64
. They do exactly what you expect them to do.
Example usage in TypeScript:
import { compressToUTF16, decompressFromUTF16 } from "async-lz-string";
async function compressAndDecompress(str: string) {
const compressed = await compressToUTF16(str);
const decompressed = await decompressFromUTF16(compressed);
console.log(str === decompressed); // true
}
If you want to change something in async-lz-string you are welcome to send a pull request.
To get started, run the following in your terminal:
git clone https://github.com/jackbister/async-lz-string.git
cd async-lz-string
npm install
npm run watch
You will now have the project and all its dependencies checked out, and webpack will be running in watch mode so your changes will be reflected immediately.
There are currently two ways you can test your changes:
npm run test
npm run start-test
npm link
in the async-lz-string directorynpm link async-lz-string
in the other project's directory.npm unlink async-lz-string
in the other project's directory and npm unlink
in the async-lz-string directory.FAQs
LZ-based compression algorithm
The npm package async-lz-string receives a total of 0 weekly downloads. As such, async-lz-string popularity was classified as not popular.
We found that async-lz-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.