Repository: cypress-io/cypress
File: packages/driver/src/cy/commands/fixtures.ts (TypeScript)

Code before:

  const reset = () => {
    cache = {}
  }

  Cypress.on('clear:fixtures:cache', reset)

  return Commands.addAll({
    fixture (fixture, ...args) {
      if (config('fixturesFolder') === false) {
        $errUtils.throwErrByPath('fixture.set_to_false')
      }

      let options: Record<string, any> = {}

      if (_.isObject(args[0])) {
        options = args[0]
      } else if (_.isObject(args[1])) {
        options = args[1]
      }

      if (_.isString(args[0]) || args[0] === null) {
        options.encoding = args[0]
      }

      const cacheKey = `${fixture}\u0000${options.encoding}`
      const cachedContent = cache[cacheKey]

      if (cachedContent) {
        // Clone the cached content to prevent accidental mutation.
        return Promise.resolve(clone(cachedContent))
      }

      const timeout = options.timeout ?? Cypress.config('responseTimeout')

      // need to remove the current timeout
      // because we're handling timeouts ourselves
      cy.clearTimeout('get:fixture')

      return Cypress.backend('get:fixture', fixture, _.pick(options, 'encoding'))
      .timeout(timeout)
      .then((response) => {
        if (response && response.__error) {
          return $errUtils.throwErr(response.__error)
        }

        // https://github.com/cypress-io/cypress/issues/1558
        // We invoke Buffer.from() in order to transform this from an ArrayBuffer -
        // which socket.io uses to transfer the file over the websocket - into a
        // `Buffer`, which webpack polyfills in the browser.
        if (options.encoding === null) {

Review comment (performance):
helps keep the cache key clean in the case the encoding option is not defined
```suggestion
      const cacheKey = `${fixture}\u0000${options.encoding || ''}`
```

Code after:

  const reset = () => {
    cache = {}
  }

  Cypress.on('clear:fixtures:cache', reset)

  return Commands.addAll({
    fixture (fixture, ...args) {
      if (config('fixturesFolder') === false) {
        $errUtils.throwErrByPath('fixture.set_to_false')
      }

      let options: Record<string, any> = {}

      if (_.isObject(args[0])) {
        options = args[0]
      } else if (_.isObject(args[1])) {
        options = args[1]
      }

      if (_.isString(args[0]) || args[0] === null) {
        options.encoding = args[0]
      }

      const cacheKey = `${fixture}\u0000${options.encoding || ''}`
      const cachedContent = cache[cacheKey]

      if (cachedContent) {
        // Clone the cached content to prevent accidental mutation.
        return Promise.resolve(clone(cachedContent))
      }

      const timeout = options.timeout ?? Cypress.config('responseTimeout')

      // need to remove the current timeout
      // because we're handling timeouts ourselves
      cy.clearTimeout('get:fixture')

      return Cypress.backend('get:fixture', fixture, _.pick(options, 'encoding'))
      .timeout(timeout)
      .then((response) => {
        if (response && response.__error) {
          return $errUtils.throwErr(response.__error)
        }

        // https://github.com/cypress-io/cypress/issues/1558
        // We invoke Buffer.from() in order to transform this from an ArrayBuffer -
        // which socket.io uses to transfer the file over the websocket - into a
        // `Buffer`, which webpack polyfills in the browser.
        if (options.encoding === null) {