npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tsxper/crc32

v2.1.3

Published

Calculates unsigned CRC32. Optimized Cycle Redundancy Check.

Downloads

1,924

Readme

CRC32

NPM Version License: MIT npm type definitions NPM Downloads

Cycle Redundancy Check 32 (CRC32).

Calculates unsigned 32 bit checksum for 0xEDB88320 polynomial.

Speed optimization is done through using pre-calculated values from lookup tables.

Release Notes.

Performance

Performance totally depends on the hardware. Results below are made on a very basic hardware with NodeJS v18.16.0, IntelCore i5 1.6 GHz CPU and DDR3 RAM.

| Processor | Rate | CRC32OPS | Input | Time | | --- | --- | --- | --- | --- | | CRC32 | 1.2047 Gbit/s | 147059 | 10K random 1024-Bytes items | 68 msec | | CRC32Streams | 850.1594 Mbit/s | - | 100MB random data file | 941 msec |

Compatibility

class CRC32
// Browser and NodeJS compatible.
class CRC32Streams
// Depends on NodeJS "stream" module.

TypeScript Support

"@tsxper/crc32" package is originally written in TypeScript. TypeScript type definitions are included.

UTF-8 Support

UTF8 characters are fully supported.

crc32.calculate('español sí');

Transitive Dependencies

None.

Example

For String

const crc32 = new CRC32();
crc32.calculate('crc32 test');
// or
crc32.forString('crc32 test');

For Uint8Array

const crc32 = new CRC32();
crc32.forBytes(new TextEncoder().encode('crc32 test'));

For Buffer

Buffer extends Uint8Array.

const crc32 = new CRC32();
crc32.forBuffer(new TextEncoder().encode('crc32 test'));

For File

const crc32 = new CRC32Streams();
const checksum = await crc32.forFile(filePath);

Calculate CRC32 For String or File In Browser

See "Compatibility".

HTML Copy/Paste Example

This example also shows how to calculate CRC32 for ArrayBuffer.

<!DOCTYPE html>
<html>
<body>
  <p>Calculating crc32 for text "hello crc32": <span id="placeholder"></span></p>
  <div>
    <input type="file" id="input" />
    <p>CRC32 of the file: <span id="filecrc32"></span></p>
  </div>
  <script type="importmap">
    {
      "imports": {
        "@tsxper/crc32": "https://www.unpkg.com/@tsxper/[email protected]/esm/CRC32.js"
      }
    }
  </script>
  <script type="module">
    import { CRC32 } from "@tsxper/crc32";
    const crc32 = new CRC32();

    function handleString() {
      const crc = crc32.calculate("hello crc32");
      document.getElementById('placeholder').innerText = crc;
    }
    handleString();

    const fileInput = document.getElementById("input");
    fileInput.addEventListener("change", handleFiles, false);

    function handleFiles(e) {
      if (!this.files.length) return;
      const file = this.files[0];
      const reader = new FileReader();
      reader.onload = (e) => {
        const buffer = e.target.result; // ArrayBuffer
        const view = new Uint8Array(buffer);
        document.getElementById('filecrc32').innerText = crc32.forBytes(view);
      };
      reader.readAsArrayBuffer(file);
    }
  </script>
</body>
</html>

Customization. Calculate for chunks, chunk by chunk.

For example, for sequence of events, converted into Uint8Array[] chunks.

const chunks = [new TextEncoder().encode('text1 text2')];
const crc32 = new CRC32();
let crc = 0;
for (const chunk of chunks) {
  crc = crc32.forBytes(chunk, crc);
}

Convert CRC32 decimal to hexadecimal

const crc32 = new CRC32();
const checksum10 = crc32.calculate("hi"); // 3633523372
const checksum16 = `0x${checksum10.toString(16)}`; // 0xd8932aac

Supporting CommonJS and ECMAScript modules

Both, CommonJS and ESM modules are supported.

import { CRC32 } from '@tsxper/crc32';
// or
const { CRC32 } = require('@tsxper/crc32');

Supporting of the both module systems is done through "import", "require" and "default" conditions.

License

MIT License.

Links