2021-05-21 22:50:40 +03:00
|
|
|
// Create web worker
|
2021-07-04 01:48:04 +03:00
|
|
|
const THRESHOLD = 10000000
|
|
|
|
const worker = new Worker('worker.js')
|
2021-05-21 22:50:40 +03:00
|
|
|
/** @type {HTMLButtonElement} */
|
2021-07-04 01:48:04 +03:00
|
|
|
const start = document.getElementById('start')
|
2021-05-21 22:50:40 +03:00
|
|
|
/** @type {HTMLParagraphElement} */
|
2021-07-04 01:48:04 +03:00
|
|
|
const status = document.getElementById('status')
|
|
|
|
const results = document.getElementById('results')
|
2021-05-21 22:50:40 +03:00
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
const ITERATIONS = 1
|
2021-05-21 22:50:40 +03:00
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
let resolver
|
2021-05-21 22:50:40 +03:00
|
|
|
|
|
|
|
const onMessage = (message) => {
|
|
|
|
// Update the UI
|
2021-07-04 01:48:04 +03:00
|
|
|
let prefix = '[Calculating]'
|
2021-05-21 22:50:40 +03:00
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
if (message.data.status === 'done') {
|
2021-05-21 22:50:40 +03:00
|
|
|
// tell tauri that we are done
|
2022-01-04 14:57:50 +03:00
|
|
|
window.__TAURI__.invoke('app_completed_successfully')
|
2021-05-21 22:50:40 +03:00
|
|
|
}
|
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
status.innerHTML = `${prefix} Found <code>${message.data.count}</code> prime numbers in <code>${message.data.time}ms</code>`
|
2021-05-21 22:50:40 +03:00
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
if (message.data.status === 'done') {
|
|
|
|
resolver(message.data.time)
|
2021-05-21 22:50:40 +03:00
|
|
|
}
|
2021-07-04 01:48:04 +03:00
|
|
|
}
|
2021-05-21 22:50:40 +03:00
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
worker.addEventListener('message', onMessage)
|
2021-05-21 22:50:40 +03:00
|
|
|
|
|
|
|
const benchmark = () => {
|
|
|
|
return new Promise((resolve) => {
|
2021-07-04 01:48:04 +03:00
|
|
|
const startTime = Date.now()
|
|
|
|
resolver = resolve
|
|
|
|
worker.postMessage({ value: THRESHOLD, startTime })
|
|
|
|
})
|
|
|
|
}
|
2021-05-21 22:50:40 +03:00
|
|
|
|
|
|
|
const calculate = async () => {
|
2021-07-04 01:48:04 +03:00
|
|
|
let total = 0
|
2021-05-21 22:50:40 +03:00
|
|
|
|
|
|
|
for (let i = 0; i < ITERATIONS; i++) {
|
2021-07-04 01:48:04 +03:00
|
|
|
const result = await benchmark()
|
|
|
|
total += result
|
2021-05-21 22:50:40 +03:00
|
|
|
}
|
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
const average = total / ITERATIONS
|
2021-05-21 22:50:40 +03:00
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
results.innerText = `Average time: ${average}ms`
|
|
|
|
}
|
2021-05-21 22:50:40 +03:00
|
|
|
|
2021-07-04 01:48:04 +03:00
|
|
|
window.addEventListener('DOMContentLoaded', calculate)
|