Skip to content

Instantly share code, notes, and snippets.

@mxdvl
Last active July 5, 2023 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxdvl/384c7055c6f33db149bae4193e4608fa to your computer and use it in GitHub Desktop.
Save mxdvl/384c7055c6f33db149bae4193e4608fa to your computer and use it in GitHub Desktop.
Benchmarking declarative & imperative loops
import { Items } from "./items.ts";
export const declarative_map = (items: Items) =>
items.map((item) => {
switch (item.type) {
case "title":
return `# ${item.content}`;
case "body":
return item.content;
case "comment":
return `> ${item.content}`;
default:
return "";
}
}).join("\n");
import { Items } from "./items.ts";
export const imperative_for_loop = (
items: Items,
) => {
let body = ``;
for (let index = 0; index < items.length; index++) {
const item = items[index];
if (!item) continue; // Typescript cannot be sure
switch (item.type) {
case "title":
body += `# ${item.content}`;
break;
case "body":
body += item.content;
break;
case "comment":
body += `> ${item.content}`;
break;
default:
break;
}
if (index < items.length - 1) body += "\n";
}
return body;
};
import { Items } from "./items.ts";
export const imperative_for_of_loop = (items: Items) => {
let body = ``;
for (const item of items) {
switch (item.type) {
case "title":
body += `# ${item.content}`;
break;
case "body":
body += item.content;
break;
case "comment":
body += `> ${item.content}`;
break;
default:
break;
}
body += "\n";
}
return body.replace(/\n$/, "");
};
export type Items = ReadonlyArray<{ type: string; content: string }>;
export const items = [
{ type: "title", content: "Prefer `Array.prototype.map`" },
{
type: "body",
content:
"We generally favour declarative programming over imperative programming.",
},
{ type: "comment", content: "I like it. -@ashishpuliyel" },
{
type: "comment",
content:
"Personally I find the `.map()` pattern easier to parse. -@bryophyta",
},
{ type: "comment", content: "map ftw 🗺️. -@cemms1" },
{ type: "comment", content: "Functors are good ✨ -@arelra" },
] as const satisfies Items;
import { declarative_map } from "./declarative_map.ts";
import { imperative_for_loop } from "./imperative_for_loop.ts";
import { imperative_for_of_loop } from "./imperative_for_of_loop.ts";
import { items } from "./items.ts";
Deno.bench("for", () => {
imperative_for_loop(items);
});
Deno.bench("for…of", () => {
imperative_for_of_loop(items);
});
Deno.bench("map", () => {
declarative_map(items);
});
import { assertEquals } from "https://deno.land/std@0.193.0/testing/asserts.ts";
import { declarative_map } from "./declarative_map.ts";
import { imperative_for_loop } from "./imperative_for_loop.ts";
import { items } from "./items.ts";
Deno.test("they are all the same", () => {
const body = declarative_map(items);
assertEquals(imperative_for_loop(items), body);
assertEquals(imperative_for_loop(items), body);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment