- Notifications
You must be signed in to change notification settings - Fork1
Fast CRC32 computation in TypeScript
License
foxglove/crc
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Fast CRC32 computation in TypeScript
ACyclic Redundancy Check (CRC) is a calculation used to detect errors in data transmission.
This library implementsCRC32, the standard 32-bit CRC using the binary polynomial0xEDB88320
. This is the same algorithm used inPNG,zlib, and other popular applications.
The following functions are exported from this package:
functioncrc32Init():number;functioncrc32Update(prev:number,data:ArrayBufferView):number;functioncrc32Final(prev:number):number;functioncrc32(data:ArrayBufferView):number;
Note: Since the CRC algorithm works with unsigned data, thecrc32
andcrc32Final
functions always returnnon-negative numbers. For example, CRC32(0x01) returns 2768625435 rather than -1526341861.
import{crc32}from"@foxglove/crc";constdata=newUint8Array(...);constcrc=crc32(data);
import{crc32Init,crc32Update,crc32Final}from"@foxglove/crc";letcrc=crc32Init();while(/* more data available */){crc=crc32Update(crc,data);}crc=crc32Final(crc);
This package achieves a >5x performance improvement over many other CRC packages, because of the multi-byte algorithms used (adapted fromhttps://github.com/komrad36/CRC).
The following benchmarks were recorded on a MacBook Pro with an M1 Pro chip and 16GB of RAM. Each iteration ("op") is processing 1MB of data.
$ yarn bench... crc: 355 ops/s, ±0.56% | 81.52% slower node-crc: 376 ops/s, ±0.14% | 80.43% slower crc-32: 1 057 ops/s, ±0.16% | 44.98% slower polycrc: 327 ops/s, ±0.21% | slowest, 82.98% slower this package: 1 921 ops/s, ±0.18% | fastest
For further information about CRCs and their computation, see:
- https://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks
- https://github.com/komrad36/CRC
- https://create.stephan-brumme.com/crc32/
- https://zlib.net/crc_v3.txt
- https://github.com/Michaelangel007/crc32
- https://docs.microsoft.com/en-us/openspecs/office_protocols/ms-abs/06966aa2-70da-4bf9-8448-3355f277cd77
@foxglove/crc is licensed under theMIT License.
- Run
yarn version --[major|minor|patch]
to bump version - Run
git push && git push --tags
to push new tag - GitHub Actions will take care of the rest
Join ourSlack channel to ask questions, share feedback, and stay up to date on what our team is working on.
About
Fast CRC32 computation in TypeScript