Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve getResolveErrorBodyCallback #2940

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions benchmarks/api/util.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { bench, group, run } from 'mitata'
import { isContentTypeText, isContentTypeApplicationJson } from '../../lib/api/util.js'

const html = 'text/html'
const json = 'application/json; charset=UTF-8'

group('isContentTypeText', () => {
bench(`isContentTypeText('${html}')`, () => {
return isContentTypeText(html)
})
bench(`isContentTypeText('${json}')`, () => {
return isContentTypeText(json)
})
bench('html.startsWith(\'text/\')', () => {
return html.startsWith('text/')
})
bench('json.startsWith(\'text/\')', () => {
return json.startsWith('text/')
})
})

group('isContentTypeApplicationJson', () => {
bench(`isContentTypeApplicationJson('${html}')`, () => {
return isContentTypeApplicationJson(html)
})
bench(`isContentTypeApplicationJson('${json}')`, () => {
return isContentTypeApplicationJson(json)
})
bench('html.startsWith(\'application/json\')', () => {
return html.startsWith('application/json')
})
bench('json.startsWith(\'application/json\')', () => {
return json.startsWith('application/json')
})
})

await run()
68 changes: 53 additions & 15 deletions lib/api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,66 @@ async function getResolveErrorBodyCallback ({ callback, body, contentType, statu
}
}

const message = `Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`

if (statusCode === 204 || !contentType || !chunks) {
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
queueMicrotask(() => callback(new ResponseStatusCodeError(message, statusCode, headers)))
return
}

try {
if (contentType.startsWith('application/json')) {
const payload = JSON.parse(chunksDecode(chunks, length))
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
return
}
const stackTraceLimit = Error.stackTraceLimit
Error.stackTraceLimit = 0
let payload

if (contentType.startsWith('text/')) {
const payload = chunksDecode(chunks, length)
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
return
try {
if (isContentTypeApplicationJson(contentType)) {
payload = JSON.parse(chunksDecode(chunks, length))
} else if (isContentTypeText(contentType)) {
payload = chunksDecode(chunks, length)
}
} catch (err) {
// Process in a fallback if error
} catch {
// process in a callback to avoid throwing in the microtask queue
} finally {
Error.stackTraceLimit = stackTraceLimit
}
queueMicrotask(() => callback(new ResponseStatusCodeError(message, statusCode, headers, payload)))
}

const isContentTypeApplicationJson = (contentType) => {
return (
contentType.length > 15 &&
contentType[11] === '/' &&
contentType[0] === 'a' &&
contentType[1] === 'p' &&
contentType[2] === 'p' &&
contentType[3] === 'l' &&
contentType[4] === 'i' &&
contentType[5] === 'c' &&
contentType[6] === 'a' &&
contentType[7] === 't' &&
contentType[8] === 'i' &&
contentType[9] === 'o' &&
contentType[10] === 'n' &&
contentType[12] === 'j' &&
contentType[13] === 's' &&
contentType[14] === 'o' &&
contentType[15] === 'n'
)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a cold error path... these kind of optimizations make it harder to maintain. I think a normal string compare is sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The valid path is 40x slower with a startsWith.

Also this function has no real complexity.


process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
const isContentTypeText = (contentType) => {
return (
contentType.length > 4 &&
contentType[4] === '/' &&
contentType[0] === 't' &&
contentType[1] === 'e' &&
contentType[2] === 'x' &&
contentType[3] === 't'
)
}

module.exports = { getResolveErrorBodyCallback }
module.exports = {
getResolveErrorBodyCallback,
isContentTypeApplicationJson,
isContentTypeText
}
Loading