Use a map for the lookup table

This commit is contained in:
Mathias Bynens
2017-12-25 07:48:09 +01:00
parent 38898a155d
commit 98ac42f3c2

View File

@@ -1,8 +1,8 @@
import posts from './_posts.js'; import posts from './_posts.js';
const lookup = {}; const lookup = new Map();
posts.forEach(post => { posts.forEach(post => {
lookup[post.slug] = JSON.stringify(post); lookup.set(post.slug, JSON.stringify(post));
}); });
export function get(req, res, next) { export function get(req, res, next) {
@@ -10,14 +10,14 @@ export function get(req, res, next) {
// is called [slug].js // is called [slug].js
const { slug } = req.params; const { slug } = req.params;
if (slug in lookup) { if (lookup.has(slug)) {
res.set({ res.set({
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Cache-Control': `max-age=${30 * 60 * 1e3}` // cache for 30 minutes 'Cache-Control': `max-age=${30 * 60 * 1e3}` // cache for 30 minutes
}); });
res.end(lookup[slug]); res.end(lookup.get(slug));
} else { } else {
next(); next();
} }
} }