Last active
July 5, 2023 09:18
-
-
Save mxdvl/384c7055c6f33db149bae4193e4608fa to your computer and use it in GitHub Desktop.
Benchmarking declarative & imperative loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$/, ""); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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