Skip to content

Commit 3d274d0

Browse files
committed
size: smaller utf16 my moving encodeCharcodes to /platform
1 parent cb9effa commit 3d274d0

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

fallback/latin1.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
nativeDecoder,
44
nativeDecoderLatin1,
55
nativeBuffer,
6+
encodeCharcodes,
67
isHermes,
78
isDeno,
89
isLE,
@@ -107,25 +108,6 @@ export const decodeAscii = nativeBuffer
107108

108109
/* eslint-disable @exodus/mutable/no-param-reassign-prop-only */
109110

110-
export const encodeCharcodes = isHermes
111-
? (str, arr) => {
112-
const length = str.length
113-
if (length > 64) {
114-
const at = str.charCodeAt.bind(str) // faster on strings from ~64 chars on Hermes, but can be 10x slower on e.g. JSC
115-
for (let i = 0; i < length; i++) arr[i] = at(i)
116-
} else {
117-
for (let i = 0; i < length; i++) arr[i] = str.charCodeAt(i)
118-
}
119-
120-
return arr
121-
}
122-
: (str, arr) => {
123-
const length = str.length
124-
// Can be optimized with unrolling, but this is not used on non-Hermes atm
125-
for (let i = 0; i < length; i++) arr[i] = str.charCodeAt(i)
126-
return arr
127-
}
128-
129111
export function encodeAsciiPrefix(x, s) {
130112
let i = 0
131113
for (const len3 = s.length - 3; i < len3; i += 4) {

fallback/platform.browser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { decodePartAddition as decodePart } from './platform.native.js'
22

3+
export { encodeCharcodesPure as encodeCharcodes } from './platform.native.js'
4+
35
export const nativeBuffer = null
46
export const isHermes = false
57
export const isDeno = false

fallback/platform.native.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,28 @@ export function decode2string(arr, start, end, m) {
9595

9696
return decodePart(arr, start, end, m)
9797
}
98+
99+
/* eslint-disable @exodus/mutable/no-param-reassign-prop-only */
100+
101+
function encodeCharcodesHermes(str, arr) {
102+
const length = str.length
103+
if (length > 64) {
104+
const at = str.charCodeAt.bind(str) // faster on strings from ~64 chars on Hermes, but can be 10x slower on e.g. JSC
105+
for (let i = 0; i < length; i++) arr[i] = at(i)
106+
} else {
107+
for (let i = 0; i < length; i++) arr[i] = str.charCodeAt(i)
108+
}
109+
110+
return arr
111+
}
112+
113+
export function encodeCharcodesPure(str, arr) {
114+
const length = str.length
115+
// Can be optimized with unrolling, but this is not used on non-Hermes atm
116+
for (let i = 0; i < length; i++) arr[i] = str.charCodeAt(i)
117+
return arr
118+
}
119+
120+
/* eslint-enable @exodus/mutable/no-param-reassign-prop-only */
121+
122+
export const encodeCharcodes = isHermes ? encodeCharcodesHermes : encodeCharcodesPure

fallback/utf16.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { decodeUCS2, encodeCharcodes } from './latin1.js'
1+
import { decodeUCS2 } from './latin1.js'
22
import { assertU8, E_STRING, E_STRICT_UNICODE } from './_utils.js'
3-
import { nativeDecoder, isLE } from './platform.js'
3+
import { nativeDecoder, isLE, encodeCharcodes } from './platform.js'
44

55
export const E_STRICT = 'Input is not well-formed utf16'
66
const isWellFormedStr = String.prototype.isWellFormed

0 commit comments

Comments
 (0)