Skip to content

Bench numbers are scary #1

Description

@doesdev

Hey, our projects are twinsies (nothing wrong with twinsies). I saw yours included a bench which was super duper helpful, because I honestly took for granted that this little step in processing requests might have too much impact.

That prompted me to pop my project in and become disappointed when I saw the numbers. Got them fixed, still not as fast as yours, or fast-url-parser, or ale, Fast-er enough than it was though ;)

Anywho, wanted to drop a note here because the benchmark numbers are a bit scary. To think that processing a request's query params would at best handle 22,603 ops/sec is troublesome. Looking at the bench you see that should be multiplied by urls.length or rather run for each and averaged, but glancing at the README might scare folks.

I've not used benchmark before, I'm sure it's far more accurate than my project but all the same here's the kind of numbers I got from my little ops-per-sec project, averaging out processing for each of the urls.

Ironically, we aren't just twinsies, there's a third tiny x params and it wins on speed.

1530854 ops/sec: tiny-url-params
1017817 ops/sec: tiny-search-params
 913690 ops/sec: querystring
 764262 ops/sec: tiny-params
 539371 ops/sec: ale-url-parser
 501211 ops/sec: query-string
 247470 ops/sec: url
 205377 ops/sec: qs
 202184 ops/sec: fast-url-parser

Here's what that bench code looks like.

'use strict'

const runFor = 1000
const opsPerSec = (fn) => require('ops-per-sec')(fn, undefined, runFor)
const ale = require('ale-url-parser').parse
const url = (u) => require('url').parse(u, true)
const queryStringNative = require('querystring').parse
const queryString = require('query-string').parse
const qs = require('qs').parse
const fastUrlParser = (u) => require('fast-url-parser').parse(u, true)
const tsp = require('tiny-search-params').parse
const tp = require('tiny-params')
const tup = require('tiny-url-params')
const urls = [
  '?string=Brother%20%22MFC-J4420DW%22%20(toner*%20tusz*%20b%C4%99ben)&utm_source=google&utm_medium=cpc&n=_onika%20-%20Komputery%20-%20Drukarki%20i%20Skanery&r=Elektronika%20-%20Komputery%20-%20Drukarki%20i%20Skanery%20-%20Tusze%20i%20tonery%20-%20Brother%20MFC-J4420DW&n=Brother%20%2BMFC-J4420DW%20%2Btusz&gclid=Tw_kE&g=aw.s&d=L0A&order=d&h=ssce-ki-5-g-2-0328&price_from=40',
  '?pr=CUSTOMIZE_IMG_Buty%2520Gazelle%2520Shoes',
  '?q=decodeURIComponent+is+slow&oq=decodeURIComponent+is+slow&aqs=chrome..69i57.1616j0j7&sourceid=chrome&ie=UTF-8',
  '?a=http%3A%2F%2Fdomain.lol%2Ffoo%2Fbar%3Ffoo%3D1&bar=2',
  '//domain.ninja/foo/bar/baz?route=https%3A%2F%2Fapi.domain.ninja%2Ffoo%3Fbar%5B%5D%3D1&bar%5B%5D=2&bar%5B%5D=3&lorem=1&ipsu%3Dm=nothing#hash',
  '?order=m&stan=nowe&dostawa-kurier=1&price_from=11&price_to=22&freeShipping=1&super-sprzedawca=1&city=Gda%C5%84sk&startingTime=3',
  '?foo=bar&baz=qux&order=m&stan=nowe&dostawa-kurier=1&price_from=11&price_to=22&freeShipping=1&super-sprzedawca=1&city=Gda%C5%84sk&startingTime=3&a[]=1&a[]=2&a[]=3&a[]=4&a[]=5&a[]=6&a[]=7&a[]=8&b[]=1&b%3d[]=2&b[]=3&b%3d[]=4&b[]=5&b%3d[]=6&b[]=7&b%3d[]=8&route=https://domain.lol/foo/bar/baz/?advert=1&route=some-other-route#hash-bash-mome-long-and-ugly____--%3F--ha-s-h',
  '?foo=bar&baz=qux&order=m&stan=nowe&dostawa-kurier=1&price_from=11&price_to=22&freeShipping=1&super-sprzedawca=1&city=Gda%C5%84sk&startingTime=3&a%5B%5D=1&a%5B%5D=2&a%5B%5D=3&a%5B%5D=4&a%5B%5D=5&a%5B%5D=6&a%5B%5D=7&a%5B%5D=8&b%5B%5D=1&b%5B%5D=3&b%5B%5D=5&b%5B%5D=7&b%3D%5B%5D=2&b%3D%5B%5D=4&b%3D%5B%5D=6&b%3D%5B%5D=8&route=https%3A%2F%2Fdomain.lol%2Ffoo%2Fbar%2Fbaz%2F%3Fadvert%3D1&route=some-other-route#hash-bash-mome-long-and-ugly____--%3F--ha-s-h',
  '?something=simple',
  '?'
]

const parsers = [
  ['tiny-search-params', tsp],
  ['fast-url-parser', fastUrlParser],
  ['tiny-url-params', tup],
  ['ale-url-parser', ale],
  ['query-string', queryString],
  ['querystring', queryStringNative],
  ['tiny-params', tp],
  ['url', url],
  ['qs', qs]
]

const bench = async () => {
  const results = []

  for (const [name, fn] of parsers) {
    console.log(`Benchmarking ${name}...`)

    let ops = 0
    for (const u of urls) {
      ops += await opsPerSec(() => fn(u))
    }

    ops = parseInt(ops / urls.length, 10)
    results.push({ name, ops })
  }

  results.sort((a, b) => b.ops - a.ops)

  const max = `${results[0].ops}`.length

  console.log('')
  results.forEach((r) => {
    const pad = `${Array((max - `${r.ops}`.length) * 2).join(' ')}`
    console.log(`${pad}${r.ops} ops/sec: ${r.name}`)
  })
}

bench()

Anyway

The point is just you may want to see if there's a way to get an output from benchmark that reflects the number of url's being processed for each op. As, the current numbers are less than most frameworks take to process and return each request in full.

Thanks for thinking about performance. This is an area that really does need to be performant and I hadn't even given it any thought.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions