bitcmp
v1.0.0
Published
Highly efficient buffer comparison in pure JS
Readme
bitcmp
Highly efficient buffer comparison in pure JS. Uses some heuristics to achieve high general case performance.
Usage
import bitcmp from "bitcmp";
const a = /* some ArrayBuffer, TypedArray or DataView */;
const b = /* another ArrayBuffer, TypedArray or DataView */;
bitcmp(a, b);
// 0 : Equal
// < 0 : A is lexicographically less than B
// > 0 : A is lexicographically greater than BTricks
Buffer#compare- Used in Node/Bun environments. This function is implemented natively, making it very fast.
IDBFactory#cmp- A hack allowed by the IndexedDB API. This is faster than the alternative for regions larger than approximately 2kB on Chrome-based browsers.
Aligned accesses
- Even when resorting to a simple loop over the buffers,
a massive speed improvement is gained by reading 4 bytes
at a time through use of an
Int32Arrayview.
- Even when resorting to a simple loop over the buffers,
a massive speed improvement is gained by reading 4 bytes
at a time through use of an
Benchmarks
Timings are shown for buffers of various sizes. This is worst-case scenario performance where every byte in each buffer must be checked.
NodeJS
| | n = 16 | n = 512 | n = 4096 | n = 16384 |
|:-------------------------------------------------------|:-------------:|:-------------:|:-------------:|:-------------:|
| bitcmp | 86.82 ns | 90.97 ns | 149.59 ns | 273.92 ns |
| bitcmp.slow | 238.03 ns | 509.99 ns | 2.47 µs | 8.41 µs |
| memcmp | 356.32 ns | 339.82 ns | 472.78 ns | 634.09 ns |
License
Copyright 2026 Xavier Pedraza
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.