Skip to content

Commit 188e257

Browse files
committed
perf: optimize decoding on byteLength <= 64
1 parent ef2aaaf commit 188e257

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

fallback/_utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,9 @@ export function decode2string(arr, start, end, m) {
128128
export function assert(condition, msg) {
129129
if (!condition) throw new Error(msg)
130130
}
131+
132+
// On arrays in heap (<= 64) it's cheaper to copy into a pooled buffer than lazy-create the ArrayBuffer storage
133+
export const toBuf = (x) =>
134+
x.byteLength <= 64 && x.BYTES_PER_ELEMENT === 1
135+
? Buffer.from(x)
136+
: Buffer.from(x.buffer, x.byteOffset, x.byteLength)

multi-byte.node.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { assertUint8 } from './assert.js'
2-
import { isDeno } from './fallback/_utils.js'
2+
import { isDeno, toBuf } from './fallback/_utils.js'
33
import { isAsciiSuperset, multibyteDecoder } from './fallback/multi-byte.js'
44
import { isAscii } from 'node:buffer'
55

6-
const toBuf = (x) => Buffer.from(x.buffer, x.byteOffset, x.byteLength)
7-
86
export function createMultibyteDecoder(encoding, loose = false) {
97
const jsDecoder = multibyteDecoder(encoding, loose) // asserts
108
let streaming = false

single-byte.node.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { assertUint8 } from './assert.js'
22
import { isAscii } from 'node:buffer'
3-
import { isDeno, isLE } from './fallback/_utils.js'
3+
import { isDeno, isLE, toBuf } from './fallback/_utils.js'
44
import { asciiPrefix } from './fallback/latin1.js'
55
import { encodingMapper, encodingDecoder, E_STRICT } from './fallback/single-byte.js'
66

7-
const toBuf = (x) => Buffer.from(x.buffer, x.byteOffset, x.byteLength)
8-
97
function latin1Prefix(arr, start) {
108
let p = start | 0
119
const length = arr.length

0 commit comments

Comments
 (0)