-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.ts
More file actions
26 lines (19 loc) · 791 Bytes
/
Copy pathworker.ts
File metadata and controls
26 lines (19 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import type { MessageData, Result, SolutionFile } from './types.ts';
self.onmessage = async ({ data }: MessageEvent<MessageData>) => {
const { solutions, parseInput } = (await import(data.path)) as SolutionFile;
const solution = solutions[data.solutionIndex];
if (!solution) {
throw new Error(`Solution function at index ${data.solutionIndex} not found.`);
}
const input = parseInput(data.input);
const performanceStart = performance.now();
const result = await Promise.resolve(solution(input));
const performanceEnd = performance.now();
const solutionPerformance = Math.round((performanceEnd - performanceStart) * 100) / 100;
const response: Result = {
index: data.solutionIndex,
performance: solutionPerformance,
result: result,
};
self.postMessage(response);
};