Skip to content

Instantly share code, notes, and snippets.

@ThatOneBro
Created September 1, 2023 21:12
Show Gist options
  • Save ThatOneBro/2b4ec74fdf43f039939f41305e07939c to your computer and use it in GitHub Desktop.
Save ThatOneBro/2b4ec74fdf43f039939f41305e07939c to your computer and use it in GitHub Desktop.
Comparison of performance of different types of `for` loops
import { bench, group, run } from "mitata";
const ENTRIES = 100000;
const OBJ_TEST = new Array(ENTRIES).fill(0).map((_) => ({
id: "Appointment.priority",
path: "Appointment.priority",
short: "Used to make informed decisions if needing to re-prioritize",
definition:
"The priority of the appointment. Can be used to make informed decisions if needing to re-prioritize appointments. (The iCal Standard specifies 0 as undefined, 1 as highest, 9 as lowest priority).",
comment:
"Seeking implementer feedback on this property and how interoperable it is.\r\rUsing an extension to record a CodeableConcept for named values may be tested at a future connectathon.",
min: 0,
max: "1",
base: { path: "Appointment.priority", min: 0, max: "1" },
type: [{ code: "unsignedInt" }],
constraint: [
{
key: "ele-1",
severity: "error",
human: "All FHIR elements must have a @value or children",
expression: "hasValue() or (children().count() > id.count())",
xpath: "@value|f:*|h:div",
source: "http://hl7.org/fhir/StructureDefinition/Element",
},
],
isModifier: false,
isSummary: false,
mapping: [
{ identity: "workflow", map: "Request.priority" },
{ identity: "w5", map: "FiveWs.class" },
{ identity: "v2", map: "ARQ-12, SCH-11.6 / TQ1-9" },
{ identity: "rim", map: ".priorityCode" },
{ identity: "ical", map: "PRIORITY" },
],
}));
const NUM_TEST = new Array(ENTRIES).fill(0).map((_, i) => i);
const STR_TEST = new Array(ENTRIES).fill(0).map((_, i) => i.toString().repeat(20));
group("For loops - objects", () => {
bench("For...of", () => {
let test;
for (const obj of OBJ_TEST) {
test = obj.constraint[0].expression;
}
});
bench("Numeric for loop", () => {
let test;
for (let i = 0; i < OBJ_TEST.length; i++) {
test = OBJ_TEST[i].constraint[0].expression;
}
});
bench(".forEach()", () => {
let test;
OBJ_TEST.forEach(obj => {
test = obj.constraint[0].expression;
});
})
});
group("For loops - nums", () => {
bench("Numeric for loop", () => {
let test;
for (let i = 0; i < NUM_TEST.length; i++) {
test = NUM_TEST[i];
}
});
bench(".forEach()", () => {
let test;
NUM_TEST.forEach(num => {
test = num;
});
});
bench("For...of", () => {
let test;
for (const num of NUM_TEST) {
test = num;
}
});
});
group("For loops - strings", () => {
bench("Numeric for loop", () => {
let test;
for (let i = 0; i < STR_TEST.length; i++) {
test = STR_TEST[i];
}
});
bench(".forEach()", () => {
let test;
STR_TEST.forEach(str => {
test = str;
});
});
bench("For...of", () => {
let test;
for (const str of STR_TEST) {
test = str;
}
});
});
await run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment