From 597d7c8b4c4bbf3c6c7d07b90e35f1745ee07c1d Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 11 Aug 2023 17:15:21 +0100 Subject: [PATCH] perf: cache author for faster lookup --- src/ts/app.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ts/app.ts b/src/ts/app.ts index 4d3cee9..bcbbf71 100644 --- a/src/ts/app.ts +++ b/src/ts/app.ts @@ -1,6 +1,7 @@ import API from "./API"; import UI from "./UI"; import Input from "./Input"; +import Author from "./Author"; const pkg = require("../../package.json"); @@ -35,12 +36,20 @@ declare const Prism: any; const blogPostContainer = UI.blogPostContainer; if (blogPostContainer) { + const authors = []; const template = Handlebars.compile(UI.blogPostTemplate.innerHTML); API.getBlogPostCount().then(async (count) => { for (let i = 0; i < count; i++) { const posts = await API.getBlogPosts(i, 5); for (const post of posts) { - const author = await API.getAuthor(post.authorId); + let author: Author; + if (authors[post.authorId]) { + author = authors[post.authorId]; + } else { + author = await API.getAuthor(post.authorId); + authors[post.authorId] = author; + } + const card = UI.createBlogPostCard(template, post, author); blogPostContainer.appendChild(card); UI.updateUI(card);