}
/**
- * Intentionally inefficient implementation.
* @param {number} n - The number of fibonacci numbers to generate.
* @returns {number} - The nth fibonacci number.
*/
const fibonacci = n => {
- if (n <= 1) return n
- return fibonacci(n - 1) + fibonacci(n - 2)
+ n = BigInt(n)
+ let current = 1n
+ let previous = 0n
+ while (--n) {
+ const tmp = current
+ current += previous
+ previous = tmp
+ }
+ return current
}
/**
- * Intentionally inefficient implementation.
* @param {number} n - The number to calculate the factorial of.
* @returns {number} - The factorial of n.
*/
const factorial = n => {
- if (n === 0) {
- return 1
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return factorial(n - 1) * n
}
const readWriteFiles = (
const poolSize = availableParallelism()
const taskExecutions = 1
const workerData = {
- function: TaskFunctions.jsonIntegerSerialization,
- taskSize: 1000
+ function: TaskFunctions.factorial,
+ taskSize: 50000
}
// FixedThreadPool
"start:esm": "node --enable-source-maps dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1",
"benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo",
- "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30"
+ "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000"
},
"keywords": [],
"author": "",
class ExpressWorker extends ClusterWorker<WorkerData, WorkerResponse> {
private static server: Server
- private static readonly factorial = (n: number): number => {
- if (n === 0) {
- return 1
+ private static readonly factorial = (n: number | bigint): bigint => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return ExpressWorker.factorial(n - 1) * n
}
private static readonly startExpress = (
application.get('/api/factorial/:number', (req: Request, res: Response) => {
const { number } = req.params
- res.send({ number: ExpressWorker.factorial(parseInt(number)) }).end()
+ res
+ .send({ number: ExpressWorker.factorial(parseInt(number)).toString() })
+ .end()
})
ExpressWorker.server = application.listen(port, () => {
"start:esm": "node --enable-source-maps dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1",
"benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo",
- "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30"
+ "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000"
},
"keywords": [],
"author": "",
Data extends ThreadWorkerData<DataPayload>,
Response extends ThreadWorkerResponse<DataPayload>
> extends ThreadWorker<Data, Response> {
- private static readonly factorial = (n: number): number => {
- if (n === 0) {
- return 1
+ private static readonly factorial = (n: number | bigint): bigint => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return RequestHandlerWorker.factorial(n - 1) * n
}
public constructor () {
factorial: (workerData?: Data) => {
return {
data: {
- number: RequestHandlerWorker.factorial(workerData!.data.number!)
+ number: RequestHandlerWorker.factorial(
+ workerData!.data.number!
+ ).toString()
}
} as unknown as Response
}
"start": "node --enable-source-maps dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1",
"benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo",
- "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30"
+ "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000"
},
"keywords": [],
"author": "",
Data extends WorkerData<BodyPayload>,
Response extends WorkerResponse<BodyPayload>
> extends ThreadWorker<Data, Response> {
- private static readonly factorial: (n: number) => number = n => {
- if (n === 0) {
- return 1
+ private static readonly factorial: (n: number | bigint) => bigint = n => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return RequestHandlerWorker.factorial(n - 1) * n
}
public constructor () {
factorial: (workerData?: Data) => {
return {
body: {
- number: RequestHandlerWorker.factorial(workerData!.body.number!)
+ number: RequestHandlerWorker.factorial(
+ workerData!.body.number!
+ ).toString()
}
} as unknown as Response
}
"start:esm": "node --enable-source-maps dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1",
"benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo",
- "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30"
+ "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000"
},
"keywords": [],
"author": "",
class FastifyWorker extends ClusterWorker<WorkerData, WorkerResponse> {
private static fastify: FastifyInstance
- private static readonly factorial = (n: number): number => {
- if (n === 0) {
- return 1
+ private static readonly factorial = (n: number | bigint): bigint => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return FastifyWorker.factorial(n - 1) * n
}
private static readonly startFastify = async (
Params: { number: number }
}>('/api/factorial/:number', request => {
const { number } = request.params
- return { number: FastifyWorker.factorial(number) }
+ return { number: FastifyWorker.factorial(number).toString() }
})
await FastifyWorker.fastify.listen({ port })
"start:esm": "node --enable-source-maps dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1",
"benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo",
- "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30"
+ "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000"
},
"keywords": [],
"author": "",
Data extends ThreadWorkerData<DataPayload>,
Response extends ThreadWorkerResponse<DataPayload>
> extends ThreadWorker<Data, Response> {
- private static readonly factorial = (n: number): number => {
- if (n === 0) {
- return 1
+ private static readonly factorial = (n: number | bigint): bigint => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return RequestHandlerWorker.factorial(n - 1) * n
}
public constructor () {
factorial: (workerData?: Data) => {
return {
data: {
- number: RequestHandlerWorker.factorial(workerData!.data.number!)
+ number: RequestHandlerWorker.factorial(
+ workerData!.data.number!
+ ).toString()
}
} as unknown as Response
}
"start": "node --enable-source-maps dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1",
"benchmark:echo": "autocannon -c 100 -d 30 -p 10 -m POST -H Content-Type=application/json -b '{\"key1\":\"value1\", \"key2\":\"value2\"}' http://localhost:8080/api/echo",
- "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/30"
+ "benchmark:factorial": "autocannon -c 100 -d 30 -p 10 http://localhost:8080/api/factorial/50000"
},
"keywords": [],
"author": "",
Data extends WorkerData<BodyPayload>,
Response extends WorkerResponse<BodyPayload>
> extends ThreadWorker<Data, Response> {
- private static readonly factorial: (n: number) => number = n => {
- if (n === 0) {
- return 1
+ private static readonly factorial: (n: number | bigint) => bigint = n => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return RequestHandlerWorker.factorial(n - 1) * n
}
public constructor () {
factorial: (workerData?: Data) => {
return {
body: {
- number: RequestHandlerWorker.factorial(workerData!.body.number!)
+ number: RequestHandlerWorker.factorial(
+ workerData!.body.number!
+ ).toString()
}
} as unknown as Response
}
)
}
for (let i = 0; i < 60; i++) {
- ws.send(JSON.stringify({ type: 'factorial', data: { number: 30 } }))
+ ws.send(JSON.stringify({ type: 'factorial', data: { number: 50000 } }))
}
})
class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
private static wss: WebSocketServer
- private static readonly factorial = (n: number): number => {
- if (n === 0) {
- return 1
+ private static readonly factorial = (n: number | bigint): bigint => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return WebSocketServerWorker.factorial(n - 1) * n
}
private static readonly startWebSocketServer = (
break
case MessageType.factorial:
ws.send(
- JSON.stringify({
- type: MessageType.factorial,
- data: {
- number: WebSocketServerWorker.factorial(data.number!)
- }
- })
+ JSON.stringify(
+ {
+ type: MessageType.factorial,
+ data: {
+ number: WebSocketServerWorker.factorial(data.number!)
+ }
+ },
+ (_, v) => (typeof v === 'bigint' ? v.toString() : v)
+ )
)
break
}
)
}
for (let i = 0; i < 60; i++) {
- ws.send(JSON.stringify({ type: 'factorial', data: { number: 30 } }))
+ ws.send(JSON.stringify({ type: 'factorial', data: { number: 50000 } }))
}
})
Data extends ThreadWorkerData<DataPayload>,
Response extends ThreadWorkerResponse<DataPayload>
> extends ThreadWorker<Data, Response> {
- private static readonly factorial = (n: number): number => {
- if (n === 0) {
- return 1
+ private static readonly factorial = (n: number | bigint): bigint => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return RequestHandlerWorker.factorial(n - 1) * n
}
public constructor () {
.execute({ data }, 'factorial')
.then(response => {
ws.send(
- JSON.stringify({
- type: MessageType.factorial,
- data: response.data
- })
+ JSON.stringify(
+ {
+ type: MessageType.factorial,
+ data: response.data
+ },
+ (_, v) => (typeof v === 'bigint' ? v.toString() : v)
+ )
)
return undefined
})
)
}
for (let i = 0; i < 60; i++) {
- ws.send(JSON.stringify({ type: 'factorial', data: { number: 30 } }))
+ ws.send(JSON.stringify({ type: 'factorial', data: { number: 50000 } }))
}
})
.execute({ data }, 'factorial')
.then(response => {
ws.send(
- JSON.stringify({
- type: MessageType.factorial,
- data: response.data
- })
+ JSON.stringify(
+ {
+ type: MessageType.factorial,
+ data: response.data
+ },
+ (_, v) => (typeof v === 'bigint' ? v.toString() : v)
+ )
)
return undefined
})
Data extends WorkerData<DataPayload>,
Response extends WorkerResponse<DataPayload>
> extends ThreadWorker<Data, Response> {
- private static readonly factorial = (n: number): number => {
- if (n === 0) {
- return 1
+ private static readonly factorial = (n: number | bigint): bigint => {
+ if (n === 0 || n === 1) {
+ return 1n
+ } else {
+ n = BigInt(n)
+ let factorial = 1n
+ for (let i = 1n; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return RequestHandlerWorker.factorial(n - 1) * n
}
public constructor () {
let result = await pool.execute({
function: TaskFunctions.fibonacci
})
- expect(result).toBe(75025)
+ expect(result).toBe(354224848179262000000)
result = await pool.execute({
function: TaskFunctions.factorial
})
let result = await pool.execute({
function: TaskFunctions.fibonacci
})
- expect(result).toBe(75025)
+ expect(result).toBe(354224848179262000000)
result = await pool.execute({
function: TaskFunctions.factorial
})
let result = await pool.execute({
function: TaskFunctions.fibonacci
})
- expect(result).toBe(75025)
+ expect(result).toBe(354224848179262000000)
result = await pool.execute({
function: TaskFunctions.factorial
})
let result = await pool.execute({
function: TaskFunctions.fibonacci
})
- expect(result).toBe(75025)
+ expect(result).toBe(354224848179262000000)
result = await pool.execute({
function: TaskFunctions.factorial
})
}
/**
- * Intentionally inefficient implementation.
* @param {number} n - The number of fibonacci numbers to generate.
* @returns {number} - The nth fibonacci number.
*/
const fibonacci = n => {
- if (n <= 1) return n
- return fibonacci(n - 1) + fibonacci(n - 2)
+ let current = 1
+ let previous = 0
+ while (--n) {
+ const tmp = current
+ current += previous
+ previous = tmp
+ }
+ return current
}
/**
- * Intentionally inefficient implementation.
* @param {number} n - The number to calculate the factorial of.
* @returns {number} - The factorial of n.
*/
const factorial = n => {
- if (n === 0) {
+ if (n === 0 || n === 1) {
return 1
+ } else {
+ let factorial = 1
+ for (let i = 1; i <= n; i++) {
+ factorial *= i
+ }
+ return factorial
}
- return factorial(n - 1) * n
}
const executeTaskFunction = data => {
case TaskFunctions.jsonIntegerSerialization:
return jsonIntegerSerialization(data.n || 100)
case TaskFunctions.fibonacci:
- return fibonacci(data.n || 25)
+ return fibonacci(data.n || 100)
case TaskFunctions.factorial:
return factorial(data.n || 100)
default: