From 0058efb2ee02e276642affe878a92bc24b15ca5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 13 Jan 2015 08:47:49 +0100 Subject: [PATCH] Remove unnecessary allocations from rest handler Correctly cache the list of `preHandlers` so that they are built only once in application lifetime. Remove array concatenation, use a new callback function instead. --- server/middleware/rest.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/server/middleware/rest.js b/server/middleware/rest.js index cfe9de77..70e3991a 100644 --- a/server/middleware/rest.js +++ b/server/middleware/rest.js @@ -23,6 +23,8 @@ module.exports = rest; */ function rest() { + var preHandlers; + return function restApiHandler(req, res, next) { var app = req.app; var restHandler = app.handler('rest'); @@ -33,8 +35,6 @@ function rest() { return res.send(app.remotes().toJSON()); } - var preHandlers; - if (!preHandlers) { preHandlers = []; var remotingOptions = app.get('remoting') || {}; @@ -58,8 +58,14 @@ function rest() { } } - async.eachSeries(preHandlers.concat(restHandler), function(handler, done) { - handler(req, res, done); - }, next); + async.eachSeries( + preHandlers, + function(handler, done) { + handler(req, res, done); + }, + function(err) { + if (err) return next(err); + restHandler(req, res, next); + }); }; }