BlogProgramming on the Toilet

PoT Episode #5: Express Middleware & Memoization

By August 6, 2019 No Comments

What is it?

Programming on the Toilet is a document which contains tips, tricks, snippets or explanations about various programming technologies and paradigms, which could help developers in their everyday job. This episode was brought to you by Edin Begić, Junior Software Engineer at Atlantbh.

Express middleware

Middleware functions are functions that have access to the request object ( ), the response object ( ), and the   function in the Express application’s request-response cycle. The   function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware in the middleware chain. If the current middleware function does not end the request-response cycle, it must call   to pass control to the next middleware function. Otherwise, the request will be left hanging.

Memoization

In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. Dynamic programming and memoization work together.

Most of the time, referring to the previous calculation output is cheaper than recomputing in terms of CPU cycles and reduces the time complexity of the solution.
There is also a handy function available in the Underscore.js and Lodash library that relies on this technique.

Want to share these tips in your workplace or at home? Download this episode of Programming on the Toilet here.