needlework
Version:
A fully automated multi-threading utility for JavaScript
60 lines (54 loc) • 1.75 kB
JavaScript
require("../dist/needlework")
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()