Skip to content

Commit

Permalink
perf: improve performance of isomorphicEncode
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Apr 12, 2024
1 parent 65f768c commit a16334d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
85 changes: 85 additions & 0 deletions benchmarks/fetch/isomorphic-encode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { bench, group, run } from 'mitata'
import { isomorphicEncode } from '../../lib/web/fetch/util.js'

const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length

function generateAsciiString (length) {
let result = ''
for (let i = 0; i < length; ++i) {
result += characters[Math.floor(Math.random() * charactersLength)]
}
return result
}

const invalidIsomorphicEncodeValueRegex = /[^\x00-\xFF]/ // eslint-disable-line

function isomorphicEncode1 (input) {
// 1. Assert: input contains no code points greater than U+00FF.
for (let i = 0; i < input.length; i++) {
if (input.charCodeAt(i) > 0xff) {
throw new TypeError('Unreachable')
}
}
// 2. Return a byte sequence whose length is equal to input’s code
// point length and whose bytes have the same values as the
// values of input’s code points, in the same order
return input
}

/**
* @see https://infra.spec.whatwg.org/#isomorphic-encode
* @param {string} input
*/
function isomorphicEncode2 (input) {
// 1. Assert: input contains no code points greater than U+00FF.
if (invalidIsomorphicEncodeValueRegex.test(input)) {
throw new TypeError('Unreachable')
}
// 2. Return a byte sequence whose length is equal to input’s code
// point length and whose bytes have the same values as the
// values of input’s code points, in the same order
return input
}

const settings = {
small: `${generateAsciiString(10)}`,
middle: `${generateAsciiString(30)}`,
long: `${generateAsciiString(70)}`
}

for (const [runName, value] of Object.entries(settings)) {
[
{ name: `${runName} (valid)`, value },
{
name: `${runName} (invalid)`,
value: `${value.slice(0, -1)}${String.fromCharCode(0xff + 1)}`
}
].forEach(({ name, value }) => {
group(name, () => {
[
{
name: 'original',
fn: isomorphicEncode
},
{
name: 'String#charCodeAt',
fn: isomorphicEncode1
},
{
name: 'RegExp#test',
fn: isomorphicEncode2
}
].forEach(({ name, fn }) => {
bench(name, () => {
try {
return fn(value)
} catch (err) {}
})
})
})
})
}

await run()
6 changes: 4 additions & 2 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1098,14 +1098,16 @@ function readableStreamClose (controller) {
}
}

const invalidIsomorphicEncodeValueRegex = /[^\x00-\xFF]/ // eslint-disable-line

/**
* @see https://infra.spec.whatwg.org/#isomorphic-encode
* @param {string} input
*/
function isomorphicEncode (input) {
// 1. Assert: input contains no code points greater than U+00FF.
for (let i = 0; i < input.length; i++) {
assert(input.charCodeAt(i) <= 0xFF)
if (invalidIsomorphicEncodeValueRegex.test(input)) {
throw new TypeError('Unreachable')
}

// 2. Return a byte sequence whose length is equal to input’s code
Expand Down

0 comments on commit a16334d

Please sign in to comment.