Skip to content

Commit

Permalink
Dependencies update + performance improvements (#37)
Browse files Browse the repository at this point in the history
* updating dependencies

* improving performance

* adding benchmarking tool
  • Loading branch information
jkyberneees authored Apr 6, 2024
1 parent 5ddb2f2 commit 5a8763c
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 16 deletions.
111 changes: 111 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const httpNext = require('./index')
const httpPrevious = require('0http')

function getReqObject (url) {
const req = {}
req.method = 'GET'
req.url = url

return req
}

function getResObject () {
const res = {}
res.statusCode = null
res.setHeader = () => {}
res.writeHead = () => {}
res.end = () => {}
return res
}

function setupRouter (router) {
router.use((req, res, next) => {
return next()
})

router.get('/', (req, res) => {
return res.end('OK')
})
router.get('/:id', (req, res) => {
return res.end(req.params.id)
})
router.get('/:id/error', () => {
throw new Error('Error')
})
}

const { router } = httpNext({
cacheSize: 0,
id: '/'
})
setupRouter(router)

const { router: routerPrevious } = httpPrevious({
cacheSize: 0
})
setupRouter(routerPrevious)

import('mitata/src/cli.mjs').then(({ run, bench, group, baseline }) => {
group('Next Router', () => {
baseline('Base URL', () => {
const req = getReqObject('/')
const res = getResObject()

router.lookup(req, res)
})
bench('Parameter URL', () => {
const req = getReqObject('/0')
const res = getResObject()

router.lookup(req, res)
})
bench('Not Found URL', () => {
const req = getReqObject('/0/404')
const res = getResObject()

router.lookup(req, res)
})
bench('Error URL', () => {
const req = getReqObject('/0/error')
const res = getResObject()

router.lookup(req, res)
})
})

group('Previous Router', () => {
baseline('Base URL', () => {
const req = getReqObject('/')
const res = getResObject()

routerPrevious.lookup(req, res)
})
bench('Parameter URL', () => {
const req = getReqObject('/0')
const res = getResObject()

routerPrevious.lookup(req, res)
})
bench('Not Found URL', () => {
const req = getReqObject('/0/404')
const res = getResObject()

routerPrevious.lookup(req, res)
})
bench('Error URL', () => {
const req = getReqObject('/0/error')
const res = getResObject()

routerPrevious.lookup(req, res)
})
})

run({
silent: false,
avg: true,
json: false,
colors: true,
min_max: false,
percentiles: false
})
})
7 changes: 4 additions & 3 deletions lib/next.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
function next (middlewares, req, res, index, routers, defaultRoute, errorHandler) {
routers = routers || {}

const middleware = middlewares[index]
if (!middleware) {
if (index >= middlewares.length) {
if (!res.finished) {
return defaultRoute(req, res)
}

return
}

const middleware = middlewares[index++]

function step (err) {
if (err) {
return errorHandler(err, req, res)
} else {
return next(middlewares, req, res, index + 1, routers, defaultRoute, errorHandler)
return next(middlewares, req, res, index, routers, defaultRoute, errorHandler)
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/router/sequential.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Trouter = require('trouter')
const next = require('./../next')
const LRU = require('lru-cache')
const { LRUCache: LRU } = require('lru-cache')
const { parse } = require('regexparam')
const queryparams = require('../utils/queryparams')

Expand Down Expand Up @@ -70,7 +70,7 @@ module.exports = (config = {}) => {
match = router.find(req.method, req.path)
}

if (match.handlers.length !== 0) {
if (match.handlers.length > 0) {
const middlewares = [...match.handlers]
if (step !== undefined) {
// router is being used as a nested router
Expand Down
20 changes: 12 additions & 8 deletions lib/utils/queryparams.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
module.exports = (req, url) => {
const [path, search = ''] = url.split('?')
const searchParams = new URLSearchParams(search.replace(/\[\]=/g, '='))

const query = {}
for (const [name, value] of searchParams.entries()) {
if (query[name]) {
Array.isArray(query[name]) ? query[name].push(value) : (query[name] = [query[name], value])
} else {
query[name] = value
const indexOfQuestionMark = url.indexOf('?')
const path = indexOfQuestionMark !== -1 ? url.slice(0, indexOfQuestionMark) : url
const search = indexOfQuestionMark !== -1 ? url.slice(indexOfQuestionMark + 1) : ''

if (search.length > 0) {
const searchParams = new URLSearchParams(search.replace(/\[\]=/g, '='))
for (const [name, value] of searchParams.entries()) {
if (query[name]) {
Array.isArray(query[name]) ? query[name].push(value) : (query[name] = [query[name], value])
} else {
query[name] = value
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
},
"homepage": "https://github.com/BackendStack21/0http#readme",
"devDependencies": {
"0http": "^3.5.2",
"@types/node": "^18.14.4",
"body-parser": "^1.20.1",
"chai": "^4.3.7",
"cross-env": "^7.0.3",
"find-my-way": "^5.2.0",
"mocha": "^10.2.0",
"mitata": "^0.1.11",
"mocha": "^10.4.0",
"nyc": "^15.1.0",
"supertest": "^6.3.3"
},
Expand All @@ -45,8 +47,8 @@
"lib/"
],
"dependencies": {
"lru-cache": "^7.18.1",
"regexparam": "^2.0.1",
"lru-cache": "^10.2.0",
"regexparam": "^3.0.0",
"trouter": "^3.2.1"
},
"types": "./index.d.ts"
Expand Down

0 comments on commit 5a8763c

Please sign in to comment.