From a3edf0019ebd3a3b8819f26893b55ad27a9399d4 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 29 Nov 2025 11:14:41 +0000 Subject: [PATCH 1/2] perf(index): use for loop over foreach --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ec3b07d..d94ad9d 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,9 @@ function replyAcceptMethod () { function fastifyAccepts (fastify, options, done) { fastify.decorateRequest('accepts', acceptsMethod) - methodNames.forEach(methodName => { + const methodNamesLength = methodNames.length + for (let i = 0; i < methodNamesLength; i+=1) { + const methodName = methodNames[i] // Defining methods this way to ensure named functions show in stack traces fastify.decorateRequest(methodName, { [methodName]: function (arr) { @@ -47,7 +49,8 @@ function fastifyAccepts (fastify, options, done) { if (options.decorateReply === true) { fastify.decorateReply('requestAccepts', replyAcceptMethod) - methodNames.forEach(methodName => { + for (let i = 0; i < methodNamesLength; i+=1) { + const methodName = methodNames[i] const capitalizedMethodName = methodName.replace(/(?:^|\s)\S/gu, a => a.toUpperCase()) const replyMethodName = 'request' + capitalizedMethodName const acceptsMethodName = 'accepts' + capitalizedMethodName From accc0ccc48d33cec18c8afc7efa32b73852ff3ca Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 29 Nov 2025 11:18:20 +0000 Subject: [PATCH 2/2] chore: fix linting --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d94ad9d..6315586 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,7 @@ function fastifyAccepts (fastify, options, done) { fastify.decorateRequest('accepts', acceptsMethod) const methodNamesLength = methodNames.length - for (let i = 0; i < methodNamesLength; i+=1) { + for (let i = 0; i < methodNamesLength; i += 1) { const methodName = methodNames[i] // Defining methods this way to ensure named functions show in stack traces fastify.decorateRequest(methodName, { @@ -44,12 +44,12 @@ function fastifyAccepts (fastify, options, done) { return acceptsObject[methodName](arr) } }[methodName]) - }) + } if (options.decorateReply === true) { fastify.decorateReply('requestAccepts', replyAcceptMethod) - for (let i = 0; i < methodNamesLength; i+=1) { + for (let i = 0; i < methodNamesLength; i += 1) { const methodName = methodNames[i] const capitalizedMethodName = methodName.replace(/(?:^|\s)\S/gu, a => a.toUpperCase()) const replyMethodName = 'request' + capitalizedMethodName @@ -62,7 +62,7 @@ function fastifyAccepts (fastify, options, done) { return acceptsObject[methodName](arr) } }[acceptsMethodName]) - }) + } } done()