Skip to content

Replace content-type with built-in util.MIMEType #4502

@climba03003

Description

@climba03003

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

I see that the core provide a MIMEType parser in the recent release which does the same as content-type but in more permissive way. For example, text/plain; application/json; charset=utf-8 is known to be wrong but allowed to parse without problem.

Although, it is slower than content-type but combine with the calculation of other property.
I believe it provide more gains.

Benchmark

const util = require('util')
const contentType = require('content-type')
const fastContentType = require('fast-content-type-parse')
const { parseContentType } = require('busboy/lib/utils')
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;

const str = 'application/json; charset=utf-8'

suite.add('util#MIMEType', function() {
    new util.MIMEType(str)
  })
  .add('content-type#parse', function() {
    contentType.parse(str)
  })
  .add('fast-content-type-parse#parse', function() {
    fastContentType.parse(str)
  })
  .add('fast-content-type-parse#safeParse', function() {
    fastContentType.safeParse(str)
  })
  .add('busboy#parseContentType', function() {
    parseContentType(str)
  })
  .on('cycle', function(event) {
    console.log(String(event.target));
  })
  .on('complete', function() {
    console.log('Fastest is ' + this.filter('fastest').map('name'));
  })
  .run({ 'async': true });

Result

util#MIMEType x 1,206,781 ops/sec ±0.22% (96 runs sampled)
fast-content-type-parse#parse x 3,752,236 ops/sec ±0.42% (96 runs sampled)
fast-content-type-parse#safeParse x 3,675,645 ops/sec ±1.09% (94 runs sampled)
content-type#parse x 1,452,582 ops/sec ±0.37% (95 runs sampled)
busboy#parseContentType x 924,306 ops/sec ±0.43% (94 runs sampled)
Fastest is fast-content-type-parse#parse

Motivation

No response

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    semver-majorIssue or PR that should land as semver major

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions