nv-buf-resize-util
v1.0.0
Published
[](https://www.npmjs.com/package/nv-buf-resize-util) [](https://www.npmjs.com/package/nv-buf-resize-util) [
const src = new Uint8Array([1,2,3,4])
const expanded = resize.expand(src,2)
const shrunk = resize.shrink(src,1)
const resized = resize(src,10)
console.log(expanded.length) // 6
console.log(shrunk.length) // 3
console.log(resized.length) // 10API
resize(src, unit_cnt, cfg?)
Resize a typed array to a specific unit count.
new_length = stride * unit_cntExample:
const resize = require("nv-buf-resize-util")
const a = new Uint8Array([1,2,3])
const b = resize(a,10)
console.log(b.length) // 10expand(src, incr_unit_cnt, cfg?)
Increase array size.
new_length = src.length + stride * incr_unit_cntExample:
const { expand } = require("nv-buf-resize-util")
const a = new Uint8Array([1,2,3])
const b = expand(a,2)
console.log(b.length) // 5shrink(src, decr_unit_cnt, cfg?)
Decrease array size.
new_length = src.length - stride * decr_unit_cntExample:
const { shrink } = require("nv-buf-resize-util")
const a = new Uint8Array([1,2,3,4])
const b = shrink(a,1)
console.log(b.length) // 3Configuration
All operations accept an optional configuration object.
Example:
const resize = require("nv-buf-resize-util")
const cfg = {
stride: 2,
aloc_when_shirnk: false
}
const buf = new Uint8Array([1,2,3,4])
const out = resize(buf,10,cfg)Configuration options:
| option | description |
|------|-------------|
| aloc | custom allocation function |
| copy | custom copy function |
| stride | element stride |
| aloc_when_shirnk | allocate new buffer when shrinking |
| expand_ratio | expansion hint |
Default configuration:
{
aloc: (new_len,src)=>new src.constructor(new_len),
copy: (src,dst)=>dst.set(src,0),
stride: 1,
aloc_when_shirnk: true,
expand_ratio: 0.5
}Internal APIs
These functions skip config normalization and are slightly faster.
_expand_shrink
Example:
const resize = require("nv-buf-resize-util")
const cfg = resize.fmt_cfg()
const out = resize._expand(new Uint8Array([1,2,3]),1,cfg)Performance
Benchmark environment:
- Node.js
- rounds = 10,000,000
Results:
expand 3186.00 ms 3138731 ops/s
expand stride 2643.44 ms 3782945 ops/s
shrink 7966.60 ms 1255240 ops/s
shrink no realloc 6662.46 ms 1500947 ops/s
resize expand 3235.88 ms 3090347 ops/s
resize shrink 8168.45 ms 1224222 ops/s
resize equal 2484.73 ms 4024583 ops/s
_expand internal 2014.37 ms 4964340 ops/s
_shrink internal 7393.33 ms 1352570 ops/sNotes:
- expand is usually faster than shrink
- shrinking may involve additional memory operations
- internal APIs avoid config normalization overhead
When to Use
This utility is useful when implementing:
- dynamic buffers
- binary parsers
- streaming decoders
- ring buffers
- custom memory pools
- high performance data pipelines
Design Philosophy
The library focuses on:
- minimal abstraction
- predictable behavior
- high performance
- small code size
It does one thing only: resizing TypedArrays safely and efficiently.
License
ANY
