needlework
Version:
A fully automated multi-threading utility for JavaScript
77 lines (68 loc) • 2.69 kB
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Needlework</title>
<script src="../dist/needlework.js"></script>
<script>
window.onload = () => {
function someHeavyFunction(numberOne, numberTwo) {
const start = Date.now()
while (start + 500 > Date.now()) { }
return numberOne + numberTwo
}
//Single execution test
const singleExecution = async () => {
const promises = []
console.time("Threaded Work - Single Execution")
for (let i = 0; i < 50; i++) {
promises.push(someHeavyFunction.runThread(i, i).then(result => {
console.log("Thread Completed: " + i)
}))
}
await Promise.all(promises)
console.timeEnd("Threaded Work - Single Execution")
someHeavyFunction.terminateThreads()
}
//Multi Execution Test
const multiExectution = async () => {
const promises = []
console.time("Threaded Work - Multi Execution")
for (let i = 0; i < 50; i += 3) {
promises.push(someHeavyFunction.runManyInThread([
[i, i],
[i + 1, i + 1],
[i + 2, i + 2]
]).then(result => {
console.log(`Threads Completed: ${i} ${i + 1} ${i + 2}`)
}))
}
await Promise.all(promises)
console.timeEnd("Threaded Work - Multi Execution")
someHeavyFunction.terminateThreads()
}
//Dependency execution test
const innerReference = () => "World"
const reference = () => "Hello " + innerReference()
const functionWithReference = () => {
return reference()
}
functionWithReference._needlework = { dependencies: { reference } }
reference._needlework = { dependencies: { innerReference } }
const dependencyExecution = async () => {
await functionWithReference.runThread()
functionWithReference.terminateThreads()
}
const run = async () => {
await singleExecution()
await multiExectution()
await dependencyExecution()
}
run()
}
</script>
</head>
<body>
</body>
</html>