forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of running hooks (fastify#714)
* Improve performance of running hooks Replace fast-iterator with a custom hook runner inside this repo. This improves performance when there are asynchronous hooks by 17-20%. * Move benchmark into new "benchmarks" folder * Add hooks benchmark for async-await * Address comments
- Loading branch information
Showing
9 changed files
with
412 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
const fastify = require('../fastify')() | ||
|
||
const opts = { | ||
schema: { | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
hello: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function promiseFunction (resolve) { | ||
setImmediate(resolve) | ||
} | ||
|
||
async function asyncHook () { | ||
await new Promise(promiseFunction) | ||
} | ||
|
||
fastify | ||
.addHook('onRequest', asyncHook) | ||
.addHook('onRequest', asyncHook) | ||
.addHook('preHandler', asyncHook) | ||
.addHook('preHandler', asyncHook) | ||
.addHook('preHandler', asyncHook) | ||
.addHook('onSend', asyncHook) | ||
|
||
fastify.get('/', opts, function (request, reply) { | ||
reply.send({ hello: 'world' }) | ||
}) | ||
|
||
fastify.listen(3000, function (err) { | ||
if (err) { | ||
throw err | ||
} | ||
console.log(`server listening on ${fastify.server.address().port}`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
const fastify = require('../fastify')() | ||
|
||
const opts = { | ||
schema: { | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
hello: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fastify | ||
.addHook('onRequest', function (req, res, next) { | ||
next() | ||
}) | ||
.addHook('onRequest', function (req, res, next) { | ||
next() | ||
}) | ||
|
||
fastify | ||
.addHook('preHandler', function (request, reply, next) { | ||
next() | ||
}) | ||
.addHook('preHandler', function (request, reply, next) { | ||
setImmediate(next) | ||
}) | ||
.addHook('preHandler', function (request, reply, next) { | ||
next() | ||
}) | ||
|
||
fastify | ||
.addHook('onSend', function (request, reply, payload, next) { | ||
next() | ||
}) | ||
|
||
fastify.get('/', opts, function (request, reply) { | ||
reply.send({ hello: 'world' }) | ||
}) | ||
|
||
fastify.listen(3000, function (err) { | ||
if (err) { | ||
throw err | ||
} | ||
console.log(`server listening on ${fastify.server.address().port}`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
function hookRunner (functions, context) { | ||
functions = functions.map(fn => fn.bind(context)) | ||
|
||
return function runHooks (runner, state, cb) { | ||
var i = 0 | ||
|
||
function next (err, value) { | ||
if (err) { | ||
cb(err, state) | ||
return | ||
} | ||
|
||
if (value !== undefined) { | ||
state = value | ||
} | ||
|
||
if (i === functions.length) { | ||
cb(null, state) | ||
return | ||
} | ||
|
||
const result = runner(functions[i++], state, next) | ||
if (result && typeof result.then === 'function') { | ||
result.then(handleResolve, handleReject) | ||
} | ||
} | ||
|
||
function handleResolve (value) { | ||
next(null, value) | ||
} | ||
|
||
function handleReject (err) { | ||
cb(err, state) | ||
} | ||
|
||
next() | ||
} | ||
} | ||
|
||
module.exports = hookRunner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.