Skip to content

Commit 6160cc0

Browse files
authored
perf: 2.5x smaller utf8.js in browsers (#61)
1 parent 1e68411 commit 6160cc0

File tree

8 files changed

+25
-9
lines changed

8 files changed

+25
-9
lines changed

fallback/utf8.auto.browser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const decodeFast = null
2+
export const encode = null

fallback/utf8.auto.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { decodeFast, encode } from './utf8.js'

fallback/utf8.auto.native.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { decodeFast, encode } from './utf8.js'

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
"/fallback/single-byte.js",
8585
"/fallback/utf16.js",
8686
"/fallback/utf8.js",
87+
"/fallback/utf8.auto.js",
88+
"/fallback/utf8.auto.browser.js",
89+
"/fallback/utf8.auto.native.js",
8790
"/array.js",
8891
"/array.d.ts",
8992
"/assert.js",
@@ -216,8 +219,12 @@
216219
"default": "./wif.js"
217220
}
218221
},
222+
"browser": {
223+
"./fallback/utf8.auto.js": "./fallback/utf8.auto.browser.js"
224+
},
219225
"react-native": {
220-
"./encoding-browser.js": "./encoding-browser.native.js"
226+
"./encoding-browser.js": "./encoding-browser.native.js",
227+
"./fallback/utf8.auto.js": "./fallback/utf8.auto.native.js"
221228
},
222229
"peerDependencies": {
223230
"@noble/hashes": "^1.8.0 || ^2.0.0"

tests/utf8.hermes.test.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
delete globalThis.TextDecoder
1+
if (!process.env.EXODUS_TEST_IS_BROWSER) delete globalThis.TextDecoder
22
delete String.prototype.isWellFormed
33
delete String.prototype.toWellFormed
44

tests/utf8.noenc.test.cjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
delete globalThis.TextEncoder
2-
delete globalThis.TextDecoder
3-
require('./utf8.lib.test.js')
1+
if (process.env.EXODUS_TEST_IS_BROWSER) {
2+
require('node:test').test.skip('Under browsers, TextEncoder / TextDecoder is required')
3+
} else {
4+
delete globalThis.TextEncoder
5+
delete globalThis.TextDecoder
6+
require('./utf8.lib.test.js')
7+
}

tests/wpt/fallback.test.cjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Drop available native implementations so fallbacks will be used
2-
// The only exception is *.node.js impls, but tests in other contexts cover that
3-
if (!globalThis.Buffer || globalThis.Buffer.TYPED_ARRAY_SUPPORT) {
2+
// The exceptions for that are *.node.js impls and browser bundles, but tests in other contexts cover that
3+
const isNode = globalThis.Buffer && !globalThis.Buffer.TYPED_ARRAY_SUPPORT
4+
if (!process.env.EXODUS_TEST_IS_BROWSER && !isNode) {
45
delete globalThis.TextEncoder
56
delete globalThis.TextDecoder
67
}

utf8.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { typedView } from './array.js'
22
import { nativeDecoder, nativeEncoder, E_STRING, E_STRICT_UNICODE } from './fallback/_utils.js'
3-
import * as js from './fallback/utf8.js'
3+
import * as js from './fallback/utf8.auto.js'
44

55
const { TextDecoder } = globalThis
66
// ignoreBOM: true means that BOM will be left as-is, i.e. will be present in the output
@@ -29,7 +29,7 @@ function deLoose(str, loose, res) {
2929
start = pos + 1
3030
if (res[pos + 1] === 0xbf && res[pos + 2] === 0xbd) {
3131
// Found a replacement char in output, need to recheck if we encoded the input correctly
32-
if (!nativeDecoder && str.length < 1e7) {
32+
if (js.decodeFast && !nativeDecoder && str.length < 1e7) {
3333
// This is ~2x faster than decode in Hermes
3434
try {
3535
if (encodeURI(str) !== null) return res // guard against optimizing out

0 commit comments

Comments
 (0)