fastify-url-data icon indicating copy to clipboard operation
fastify-url-data copied to clipboard

replace fast-uri url parser with URL contructor

Open synapse opened this issue 1 year ago • 11 comments

Fixes #141

N.B. this fix replaces the current URIComponent with URL.

Checklist

synapse avatar Mar 12 '24 14:03 synapse

I think there was an unanswered question in the referenced issue. Has that been addressed?

jsumners avatar Mar 12 '24 15:03 jsumners

Oh, that’s true, it might need a benchmark

gurgunday avatar Mar 12 '24 17:03 gurgunday

We should really have a benchmark to check if the breakage is worth the change.

mcollina avatar Mar 13 '24 08:03 mcollina

Here are the benchmarks:

Code used

const fastUri = require('fast-uri');
const benchmark = require('benchmark')

const url = 'uri://user:[email protected]:123/one/two.three?q1=a1&q2=a2#body';

new benchmark.Suite()
  .add('fast-uri', function () {
    fastUri.parse(url)
  })
  .add('url', function () {
    new URL(url)
  })
  .on('cycle', function (event) {
    console.log(String(event.target));
  })
  .on('complete', function () {
    console.log('Fastest is ' + this.filter('fastest').map('name'));
  })
  .run({ async: true });

Results run on Node version v20.11.1 LTS:

fast-uri x 2,588,130 ops/sec ±1.30% (95 runs sampled)
url x 2,910,301 ops/sec ±0.87% (93 runs sampled)
Fastest is url

fast-uri x 2,625,725 ops/sec ±0.36% (98 runs sampled)
url x 3,026,747 ops/sec ±0.33% (94 runs sampled)
Fastest is url

fast-uri x 2,651,826 ops/sec ±0.41% (98 runs sampled)
url x 3,029,741 ops/sec ±0.51% (92 runs sampled)
Fastest is url

Results run on Node version v18.19.1:

fast-uri x 2,522,467 ops/sec ±0.78% (97 runs sampled)
url x 2,730,338 ops/sec ±0.47% (94 runs sampled)
Fastest is url

fast-uri x 2,511,920 ops/sec ±0.73% (96 runs sampled)
url x 2,664,369 ops/sec ±0.45% (98 runs sampled)
Fastest is url

fast-uri x 2,486,801 ops/sec ±0.44% (97 runs sampled)
url x 2,700,008 ops/sec ±0.40% (96 runs sampled)
Fastest is url

synapse avatar Mar 13 '24 12:03 synapse

Performance on node18?

Uzlopak avatar Mar 13 '24 13:03 Uzlopak

As shown in other issues, it's important to actually benchmark the actual module, not the function in isolation.

mcollina avatar Mar 14 '24 06:03 mcollina

You're absolutely right @mcollina I am already working on that as well. I'll also post the results soon.

synapse avatar Mar 14 '24 07:03 synapse

Here is the benchmarks using it in fastify:

code used:


const benchmark = require('benchmark')
const Fastify = require('fastify')
const http = require('node:http')
const pluginFast = require('./plugin-fast');
const pluginUrl = require('../plugin');

async function getFastify(plugin, port) {
  const fastify = Fastify()
  fastify
    .register(plugin)
    .after((err) => {
      if (err) console.error(err)
    })
  
  fastify.get("/one", (req, reply) => {
    const uriData = req.urlData()
    reply.send()
  })

  try {
    await fastify.listen({ port })    
  } catch (err) {
    process.exit(1)
  }

  return fastify;
}

const bench = async () => {
  const fastifyPluginOld = await getFastify(pluginFast, 3201);
  const fastifyPluginNew = await getFastify(pluginUrl, 3202);

  new benchmark.Suite()
    .add('fast-uri', async function () {
      await fastifyPluginOld.inject({
        method: "GET",
        url: "/one?a=b&c=d#foo",
      })
    })
    .add('url', async function () {
      await fastifyPluginNew.inject({
            method: "GET",
            url: "/one?a=b&c=d#foo",
          })
    })
    .on('cycle', function (event) {
      console.log(String(event.target));
    })
    .on('complete', function () {
      console.log('Fastest is ' + this.filter('fastest').map('name'));
      fastifyPluginOld.close()
      fastifyPluginNew.close()
    })
    .run({async: true});
}

bench();

Results run on Node version v20.11.1 LTS:

fast-uri x 1,171,424 ops/sec ±8.73% (58 runs sampled)
url x 1,172,971 ops/sec ±7.79% (49 runs sampled)
Fastest is url,fast-uri

Results run on Node version v18.19.1:

fast-uri x 1,388,169 ops/sec ±18.89% (50 runs sampled)
url x 1,366,116 ops/sec ±20.89% (37 runs sampled)
Fastest is fast-uri,url

synapse avatar Mar 14 '24 14:03 synapse

Given it has no significant gain, I don't think changing the module API is worth it.

mcollina avatar Mar 19 '24 09:03 mcollina

@mcollina reducing amount of dependencies is not a good thing on its own?

kibertoad avatar Mar 19 '24 09:03 kibertoad

But it's not. fast-uri is a dependency of fastify and it would keep being one.

mcollina avatar Mar 19 '24 09:03 mcollina

Given lack of movement, and the conclusions by Matteo, I am closing this.

jsumners avatar Jun 26 '24 10:06 jsumners