Skip to content

Instantly share code, notes, and snippets.

@varenya
Created August 19, 2024 12:42
Show Gist options
  • Save varenya/f5d31a357bd8ade6b1328327e479ef4f to your computer and use it in GitHub Desktop.
Save varenya/f5d31a357bd8ade6b1328327e479ef4f to your computer and use it in GitHub Desktop.
type Task = {
name: string;
time: number;
event: "start" | "end";
};
function calculateExecutionTimes(tasks: Task[]) {
const initialTaskMap: { [name: string]: { start: number; end: number } } = {};
const groupedTasks = tasks.reduce((acc, curr) => {
const currValue = acc[curr.name] || {};
if (curr.event == "start") {
return { ...acc, [curr.name]: { ...currValue, start: curr.time } };
} else {
return { ...acc, [curr.name]: { ...currValue, end: curr.time } };
}
}, initialTaskMap);
const taskValues = Object.keys(groupedTasks).map((task) => {
const timeTaken = groupedTasks[task];
return { [task]: timeTaken.end - timeTaken.start };
});
return taskValues;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment