Repository: fastify/fastify
File: lib/validation.js (JavaScript)

Code before:
}

function wrapValidationError (result, dataVar, schemaErrorFormatter) {
  if (result instanceof Error) {
    result.statusCode = result.statusCode || 400
    result.code = result.code || 'FST_ERR_VALIDATION'
    result.validationContext = result.validationContext || dataVar
    return result
  }

  const error = schemaErrorFormatter(result, dataVar)
  error.statusCode = error.statusCode || 400
  error.code = error.code || 'FST_ERR_VALIDATION'
  error.validation = result
  error.validationContext = dataVar
  return error
}

/**
 * simple function to retrieve the essence media type
 * @param {string} header
 * @returns {string} Mimetype string.
 */
function getEssenceMediaType (header) {
  if (!header) return ''
  return header.split(';', 1)[0].trim().toLowerCase()
}

module.exports = {
  symbols: { bodySchema, querystringSchema, responseSchema, paramsSchema, headersSchema },
  compileSchemasForValidation,
  compileSchemasForSerialization,
  validate
}


Review comment (performance):
We _shouldn't_ ever see headers written like this (https://httpwg.org/specs/rfc9110.html#media.type). The spec is `<type>[;][<parameter>]`. But I suppose we have to handle this case due to jerks. However, it feel like a slippery slope to me.

Anyway, I think we might be able to eek out some more performance with `slice` instead:

```js
'use strict'

const iterations = 1_000_000
const hrtime = process.hrtime.bigint
const fixture = 'application/json foo; charset=utf-8'

const startSplit = hrtime()
for (var i = 0; i < iterations; i += 1) {
  const result = fixture.split(/[ ;]/, 1)[0].trim().toLowerCase()
  doSomething(result)
}
const endSplit = hrtime()

const startSlice = hrtime()
for (var i = 0; i < iterations; i += 1) {
  const result = fixture.slice(
    0, fixture.indexOf(' ')
  ).replace(';', '').trim().toLowerCase()
  doSomething(result)
}
const endSlice = hrtime()

const splitTime = endSplit - startSplit
console.log('split:', splitTime)

const sliceTime = endSlice - startSlice
console.log('slice:', sliceTime)

console.log('split > slice =', splitTime > sliceTime)

function doSomething(value) {
  return value >> 1
}
```

Code after:
}

function wrapValidationError (result, dataVar, schemaErrorFormatter) {
  if (result instanceof Error) {
    result.statusCode = result.statusCode || 400
    result.code = result.code || 'FST_ERR_VALIDATION'
    result.validationContext = result.validationContext || dataVar
    return result
  }

  const error = schemaErrorFormatter(result, dataVar)
  error.statusCode = error.statusCode || 400
  error.code = error.code || 'FST_ERR_VALIDATION'
  error.validation = result
  error.validationContext = dataVar
  return error
}

/**
 * simple function to retrieve the essence media type
 * @param {string} header
 * @returns {string} Mimetype string.
 */
function getEssenceMediaType (header) {
  if (!header) return ''
  return header.split(/[ ;]/, 1)[0].trim().toLowerCase()
}

module.exports = {
  symbols: { bodySchema, querystringSchema, responseSchema, paramsSchema, headersSchema },
  compileSchemasForValidation,
  compileSchemasForSerialization,
  validate
}