Repository: discordjs/discord.js
File: packages/discord.js/src/managers/EntitlementManager.js (JavaScript)

Code before:
  async fetch(options) {
    if (!options) return this._fetchMany(options);
    const { entitlement, cache, force } = options;
    const resolvedEntitlement = this.resolveId(entitlement ?? options);

    if (resolvedEntitlement) {
      return this._fetchSingle({ entitlement: resolvedEntitlement, cache, force });
    }

    return this._fetchMany(options);
  }

  async _fetchSingle({ entitlement, cache, force = false }) {
    if (!force) {
      const existing = this.cache.get(entitlement);

      if (existing) {
        return existing;
      }
    }

    const data = await this.client.rest.get(Routes.entitlement(this.client.application.id, entitlement));
    return this._add(data, cache);
  }

  async _fetchMany({ limit, guild, user, skus, excludeEnded, excludeDelete, cache, before, after } = {}) {
    const query = makeURLSearchParams({
      limit,
      guild_id: guild && this.client.guilds.resolveId(guild),
      user_id: user && this.client.users.resolveId(user),
      sku_ids: skus?.map(sku => resolveSKUId(sku)).join(','),
      exclude_ended: excludeEnded,
      exclude_deleted: excludeDelete,
      before,
      after,
    });

    const entitlements = await this.client.rest.get(Routes.entitlements(this.client.application.id), { query });

    return entitlements.reduce(
      (coll, entitlement) => coll.set(entitlement.id, this._add(entitlement, cache)),
      new Collection(),
    );
  }

  /**
   * Options used to create a test entitlement
   * <info>Either `guild` or `user` must be provided, but not both</info>
   * @typedef {Object} EntitlementCreateOptions
   * @property {SKUResolvable} sku The id of the SKU to create the entitlement for
   * @property {GuildResolvable} [guild] The guild to create the entitlement for

Review comment (performance):
```suggestion
  async _fetchMany({ limit, guild, user, skus, excludeEnded, excludeDeleted, cache, before, after } = {}) {
```

Code after:
  async fetch(options) {
    if (!options) return this._fetchMany(options);
    const { entitlement, cache, force } = options;
    const resolvedEntitlement = this.resolveId(entitlement ?? options);

    if (resolvedEntitlement) {
      return this._fetchSingle({ entitlement: resolvedEntitlement, cache, force });
    }

    return this._fetchMany(options);
  }

  async _fetchSingle({ entitlement, cache, force = false }) {
    if (!force) {
      const existing = this.cache.get(entitlement);

      if (existing) {
        return existing;
      }
    }

    const data = await this.client.rest.get(Routes.entitlement(this.client.application.id, entitlement));
    return this._add(data, cache);
  }

  async _fetchMany({ limit, guild, user, skus, excludeEnded, excludeDeleted, cache, before, after } = {}) {
    const query = makeURLSearchParams({
      limit,
      guild_id: guild && this.client.guilds.resolveId(guild),
      user_id: user && this.client.users.resolveId(user),
      sku_ids: skus?.map(sku => resolveSKUId(sku)).join(','),
      exclude_ended: excludeEnded,
      exclude_deleted: excludeDeleted,
      before,
      after,
    });

    const entitlements = await this.client.rest.get(Routes.entitlements(this.client.application.id), { query });

    return entitlements.reduce(
      (coll, entitlement) => coll.set(entitlement.id, this._add(entitlement, cache)),
      new Collection(),
    );
  }

  /**
   * Options used to create a test entitlement
   * <info>Either `guild` or `user` must be provided, but not both</info>
   * @typedef {Object} EntitlementCreateOptions
   * @property {SKUResolvable} sku The id of the SKU to create the entitlement for
   * @property {GuildResolvable} [guild] The guild to create the entitlement for