|
const MAX_TEST_RUNS = 10; |
|
|
|
/** |
|
* A typical DB response might look or might not look like this. |
|
* This object size and complexity is intentional. |
|
*/ |
|
const response = { |
|
id: Math.random(), |
|
name: `Complex Object ${Math.random().toString()}`, |
|
details: { |
|
description: "This is a complex object with many properties", |
|
createdAt: new Date(), |
|
updatedAt: new Date(), |
|
attributes: { |
|
color: "blue", |
|
size: "large", |
|
weight: "heavy", |
|
dimensions: { |
|
length: 100 + Math.random(), |
|
width: 50 + Math.random(), |
|
height: 75 + Math.random(), |
|
}, |
|
materials: ["metal", "plastic", "wood"], |
|
tags: ["tag1", "tag2", "tag3"], |
|
status: "active", |
|
metadata: { |
|
version: "1.0.0", |
|
author: "Author Name", |
|
license: "MIT", |
|
links: [ |
|
{ type: "website", url: "https://example.com" }, |
|
{ type: "repository", url: "https://github.com/example" }, |
|
], |
|
}, |
|
}, |
|
}, |
|
relationships: { |
|
parent: { id: 0, name: "Parent Object" }, |
|
children: [ |
|
{ id: 2, name: "Child Object 1" }, |
|
{ id: 3, name: "Child Object 2" }, |
|
{ id: 4, name: "Child Object 3" }, |
|
], |
|
related: [ |
|
{ id: 5, name: "Related Object 1" }, |
|
{ id: 6, name: "Related Object 2" }, |
|
], |
|
}, |
|
history: [ |
|
{ event: "created", timestamp: new Date(), by: "user1" }, |
|
{ event: "updated", timestamp: new Date(), by: "user2" }, |
|
{ event: "modified", timestamp: new Date(), by: "user3" }, |
|
], |
|
config: { |
|
settings: { |
|
option1: true, |
|
option2: false, |
|
option3: "default", |
|
thresholds: { |
|
min: 10, |
|
max: 100, |
|
default: 50, |
|
}, |
|
}, |
|
permissions: { |
|
read: ["user1", "user2"], |
|
write: ["user2"], |
|
execute: ["user3"], |
|
}, |
|
}, |
|
statistics: { |
|
views: 1024, |
|
likes: 512, |
|
shares: 256, |
|
comments: 128, |
|
ratings: { |
|
average: 4.5, |
|
total: 50, |
|
breakdown: { |
|
"1_star": 5, |
|
"2_star": 3, |
|
"3_star": 10, |
|
"4_star": 12, |
|
"5_star": 20, |
|
}, |
|
}, |
|
}, |
|
newData: { |
|
id: Math.random(), |
|
name: `Complex Object ${Math.random().toString()}`, |
|
details: { |
|
description: "This is a complex object with many properties", |
|
createdAt: new Date(), |
|
updatedAt: new Date(), |
|
attributes: { |
|
color: "blue", |
|
size: "large", |
|
weight: "heavy", |
|
dimensions: { |
|
length: 100 + Math.random(), |
|
width: 50 + Math.random(), |
|
height: 75 + Math.random(), |
|
}, |
|
materials: ["metal", "plastic", "wood"], |
|
tags: ["tag1", "tag2", "tag3"], |
|
status: "active", |
|
metadata: { |
|
version: "1.0.0", |
|
author: "Author Name", |
|
license: "MIT", |
|
links: [ |
|
{ type: "website", url: "https://example.com" }, |
|
{ type: "repository", url: "https://github.com/example" }, |
|
], |
|
}, |
|
}, |
|
}, |
|
relationships: { |
|
parent: { id: 0, name: "Parent Object" }, |
|
children: [ |
|
{ id: 2, name: "Child Object 1" }, |
|
{ id: 3, name: "Child Object 2" }, |
|
{ id: 4, name: "Child Object 3" }, |
|
], |
|
related: [ |
|
{ id: 5, name: "Related Object 1" }, |
|
{ id: 6, name: "Related Object 2" }, |
|
], |
|
}, |
|
history: [ |
|
{ event: "created", timestamp: new Date(), by: "user1" }, |
|
{ event: "updated", timestamp: new Date(), by: "user2" }, |
|
{ event: "modified", timestamp: new Date(), by: "user3" }, |
|
], |
|
config: { |
|
settings: { |
|
option1: true, |
|
option2: false, |
|
option3: "default", |
|
thresholds: { |
|
min: 10, |
|
max: 100, |
|
default: 50, |
|
}, |
|
}, |
|
permissions: { |
|
read: ["user1", "user2"], |
|
write: ["user2"], |
|
execute: ["user3"], |
|
}, |
|
}, |
|
statistics: { |
|
views: 1024, |
|
likes: 512, |
|
shares: 256, |
|
comments: 128, |
|
ratings: { |
|
average: 4.5, |
|
total: 50, |
|
breakdown: { |
|
"1_star": 5, |
|
"2_star": 3, |
|
"3_star": 10, |
|
"4_star": 12, |
|
"5_star": 20, |
|
}, |
|
}, |
|
}, |
|
}, |
|
}; |
|
|
|
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
|
|
|
/** |
|
* Each on this could be a DB query. We're also adding an artificial delay |
|
* to simulate network condition or the query latency. |
|
* |
|
* Each of this query resolves the same object in different ways |
|
* to mimic different DB query responses. |
|
*/ |
|
const q1 = async () => { |
|
await delay(100); |
|
return await Promise.resolve({ response }); |
|
}; |
|
|
|
const q2 = async () => { |
|
await delay(200); |
|
return await Promise.resolve([response, [{ response }], [{ ...response }]]); |
|
}; |
|
|
|
const q3 = async () => { |
|
await delay(300); |
|
return await Promise.resolve([ |
|
[response, response, { response, ...response }], |
|
]); |
|
}; |
|
|
|
const q4 = async () => { |
|
await delay(400); |
|
return await Promise.resolve([ |
|
[[{ response }], [{ response }], [{ response }]], |
|
]); |
|
}; |
|
|
|
// Helper to get the Start and End time of an operation. |
|
const getComputeTime = async (label, callback) => { |
|
if (!label || !callback) throw new Error("Missing arguments."); |
|
|
|
console.time(label); |
|
await callback(); |
|
console.timeEnd(label); |
|
}; |
|
|
|
const runTests = async () => { |
|
for (let i = 0; i < MAX_TEST_RUNS; i++) { |
|
console.log(`Run ${i + 1}:`); |
|
|
|
await getComputeTime("Parallel", async () => { |
|
await Promise.allSettled([q1(), q2(), q3(), q4()]); |
|
}); |
|
|
|
await getComputeTime("Sequential", async () => { |
|
await q1(); |
|
await q2(); |
|
await q3(); |
|
await q4(); |
|
}); |
|
} |
|
}; |
|
|
|
runTests(); |