@nxtedition/slice 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +29 -17
  2. package/lib/index.js +3 -0
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -45,29 +45,41 @@ Measured on Apple M3 Pro, Node.js v25.3.0:
45
45
  ### Allocation
46
46
 
47
47
  | Operation | `Buffer.allocUnsafe` | `Buffer.allocUnsafeSlow` | `PoolAllocator` | Speedup |
48
- | ---------------- | -------------------- | ------------------------ | --------------- | ------- |
49
- | alloc 64 bytes | 28.53 ns | 31.33 ns | **3.87 ns** | 7.4x |
50
- | alloc 256 bytes | 36.98 ns | 170.56 ns | **4.34 ns** | 8.5x |
51
- | alloc 1024 bytes | 65.48 ns | 238.49 ns | **4.36 ns** | 15.0x |
52
- | alloc 4096 bytes | 301.19 ns | 292.50 ns | **4.29 ns** | 70.2x |
48
+ | ---------------- | -------------------: | -----------------------: | --------------: | ------: |
49
+ | alloc 64 bytes | 38.08 ns | 41.23 ns | **5.66 ns** | 6.7x |
50
+ | alloc 256 bytes | 52.09 ns | 231.46 ns | **5.90 ns** | 8.8x |
51
+ | alloc 1024 bytes | 91.24 ns | 340.75 ns | **5.83 ns** | 15.6x |
52
+ | alloc 4096 bytes | 446.53 ns | 437.83 ns | **6.24 ns** | 71.6x |
53
+
54
+ ### Allocation (GC)
55
+
56
+ | Operation | `Buffer.allocUnsafe` | `Buffer.allocUnsafeSlow` | `PoolAllocator` | Speedup |
57
+ | ---------------- | -------------------: | -----------------------: | --------------: | ------: |
58
+ | alloc 64 bytes | 400.46 ns | 167.94 ns | **6.33 ns** | 63.3x |
59
+ | alloc 256 bytes | 309.57 ns | 500.58 ns | **6.35 ns** | 48.7x |
60
+ | alloc 4096 bytes | 653.40 ns | 620.19 ns | **6.32 ns** | 103.4x |
61
+
62
+ Under GC pressure, the advantage grows dramatically — up to **103x** faster — because `PoolAllocator` reuses slots from a pre-allocated buffer and never creates objects for V8 to trace.
53
63
 
54
64
  ### Slice creation vs `Buffer.subarray`
55
65
 
56
- | Operation | `Buffer.subarray` | `Slice` | Speedup |
57
- | ------------------- | ----------------- | ----------- | ------- |
58
- | subarray 64 bytes | 27.32 ns | **9.68 ns** | 2.8x |
59
- | subarray 1024 bytes | 26.75 ns | **9.52 ns** | 2.8x |
66
+ | Operation | `Buffer.subarray` | `Slice` | Speedup |
67
+ | ---------------------- | ----------------: | -----------: | ------: |
68
+ | subarray 64 bytes | 38.11 ns | **12.99 ns** | 2.9x |
69
+ | subarray 1024 bytes | 36.87 ns | **13.26 ns** | 2.8x |
70
+ | subarray 64 bytes (GC) | 127.30 ns | **81.60 ns** | 1.6x |
60
71
 
61
72
  ### Combined operations
62
73
 
63
- | Operation | `Buffer.subarray` | `Slice` | Speedup |
64
- | ------------------------------------- | ----------------- | ------------- | ------- |
65
- | subarray + toString (64 bytes) | 78.89 ns | **60.12 ns** | 1.3x |
66
- | alloc/free 64 bytes | 25.24 ns | **22.82 ns** | 1.1x |
67
- | alloc/free 256 bytes | 34.07 ns | **22.63 ns** | 1.5x |
68
- | realloc churn (64 → 128 → 64) | 70.20 ns | **20.80 ns** | 3.4x |
69
- | realloc in-place (grow within bucket) | 45.41 ns | **8.28 ns** | 5.5x |
70
- | 10 concurrent allocs then free | 312.96 ns | **259.34 ns** | 1.2x |
74
+ | Operation | `Buffer` | `PoolAllocator` | Speedup |
75
+ | ------------------------------------- | --------: | --------------: | ------: |
76
+ | alloc/free 64 bytes | 32.72 ns | **30.80 ns** | 1.1x |
77
+ | alloc/free 64 bytes (GC) | 273.35 ns | **73.34 ns** | 3.7x |
78
+ | alloc/free 256 bytes | 58.88 ns | **29.38 ns** | 2.0x |
79
+ | realloc churn (64 → 128 → 64) | 93.63 ns | **26.99 ns** | 3.5x |
80
+ | realloc in-place (grow within bucket) | 60.16 ns | **11.06 ns** | 5.4x |
81
+ | 10 concurrent allocs then free | 406.26 ns | **337.83 ns** | 1.2x |
82
+ | 10 concurrent allocs then free (GC) | 647.95 ns | **649.73 ns** | 1.0x |
71
83
 
72
84
  ## API
73
85
 
package/lib/index.js CHANGED
@@ -159,6 +159,9 @@ export class Slice {
159
159
  }
160
160
 
161
161
  at(index ) {
162
+ if (index >= this.byteLength || index < -this.byteLength) {
163
+ throw new RangeError(`Index out of range: ${index}`)
164
+ }
162
165
  return index >= 0
163
166
  ? this.buffer[this.byteOffset + index]
164
167
  : this.buffer[this.byteOffset + this.byteLength + index]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/slice",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -28,5 +28,5 @@
28
28
  "rimraf": "^6.1.2",
29
29
  "typescript": "^5.9.3"
30
30
  },
31
- "gitHead": "8dbd8386c6d4c511ffa81a904c58b6f648811a52"
31
+ "gitHead": "f385fe31237669bf5f9ac1fb5154e5a353710d01"
32
32
  }