From 98ac42f3c2245d5edb66c6a4fb0e14652f95011e Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Mon, 25 Dec 2017 07:48:09 +0100 Subject: [PATCH] Use a map for the lookup table --- routes/api/blog/[slug].js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/routes/api/blog/[slug].js b/routes/api/blog/[slug].js index ea975b8..8433e91 100644 --- a/routes/api/blog/[slug].js +++ b/routes/api/blog/[slug].js @@ -1,8 +1,8 @@ import posts from './_posts.js'; -const lookup = {}; +const lookup = new Map(); posts.forEach(post => { - lookup[post.slug] = JSON.stringify(post); + lookup.set(post.slug, JSON.stringify(post)); }); export function get(req, res, next) { @@ -10,14 +10,14 @@ export function get(req, res, next) { // is called [slug].js const { slug } = req.params; - if (slug in lookup) { + if (lookup.has(slug)) { res.set({ 'Content-Type': 'application/json', 'Cache-Control': `max-age=${30 * 60 * 1e3}` // cache for 30 minutes }); - res.end(lookup[slug]); + res.end(lookup.get(slug)); } else { next(); } -} \ No newline at end of file +}