-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attempt to improve performance with web workers and lodash throttling
- Loading branch information
1 parent
43e31e5
commit e6ce2e6
Showing
7 changed files
with
205 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as Comlink from "comlink"; | ||
import { aggregatorB } from "./clusteringB"; | ||
|
||
// Define the functions that will be available in the worker | ||
const workerFunctions = { | ||
aggregatorB, | ||
}; | ||
|
||
export type ClusteringWorker = typeof workerFunctions; | ||
|
||
// Expose the worker functions to the main thread | ||
Comlink.expose(workerFunctions); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { create } from "zustand"; | ||
import { useViewSettingsStore } from "./useViewSettingsStore"; | ||
import { useRawDataStore } from "./useRawDataStore"; | ||
import _ from "lodash"; | ||
import { ClusteringWorker } from "@/lib/clustering.worker"; | ||
import * as Comlink from "comlink"; | ||
|
||
import Worker from "../lib/clustering.worker?worker"; | ||
|
||
interface DataStore { | ||
aggregated: Record<string, number>[][]; | ||
yDomain: [number, number]; | ||
colsAccordingToAggregation: [string, number][]; | ||
|
||
processData: () => void; | ||
} | ||
|
||
const workerInstance = new Worker({ name: "aggregator" }); | ||
const workerApi = Comlink.wrap<ClusteringWorker>(workerInstance); | ||
|
||
export const useViewModelStore = create<DataStore>((set) => { | ||
console.log("init view model store"); | ||
|
||
const throttledDataProcess = _.throttle(async () => { | ||
console.count("Throttled process data"); | ||
const timerName = Date.now(); | ||
|
||
console.time("ViewModel process duration " + String(timerName)); | ||
const dimensions = useRawDataStore.getState().dimensions; | ||
const values = useRawDataStore.getState().values; | ||
const { updateSettings, ...presentationSettings } = | ||
useViewSettingsStore.getState(); | ||
console.log("Settings: ", presentationSettings); | ||
|
||
const aggregated = await workerApi.aggregatorB( | ||
values, | ||
dimensions, | ||
presentationSettings | ||
); | ||
console.timeEnd("ViewModel process duration " + String(timerName)); | ||
|
||
set(aggregated); | ||
}, 2000); | ||
|
||
return { | ||
aggregated: [], | ||
yDomain: [0, 10], | ||
colsAccordingToAggregation: [], | ||
|
||
processData: async () => { | ||
console.count("Process data with worker"); | ||
throttledDataProcess(); | ||
}, | ||
}; | ||
}); |
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