<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
const arr = [8, 9, 3, 9, 6, 5, 1, 7, 3, 9, 4, 1, 7, 2, 8, 3, 2, 1, 4, 7];
const small5 = [arr[0]];
const big5 = [arr[0]];
for (const n of arr.slice(1)) {
if (n < small5[4] || small5.length < 5) {
for (let i = 0; i < 5; ++i) {
if (n < small5[i] || i === small5.length - 1) {
small5.splice(i, 0, n);
break;
}
}
}
if (n > big5[4] || big5.length < 5) {
for (let i = 0; i < 5; ++i) {
if (n > big5[i]) {
big5.splice(i, 0, n);
break;
} else if (i === big5.length - 1) {
big5.push(n);
break;
}
}
}
}
let total = 0;
for (let i = 0; i < 5; ++i) {
total += small5[i] + big5[i];
}
const arr = [8, 9, 3, 9, 6, 5, 1, 7, 3, 9, 4, 1, 7, 2, 8, 3, 2, 1, 4, 7];
const small5 = [];
const big5 = [];
for (const n of arr) {
// Maintain 5 smallest numbers
if (small5.length < 5 || n < small5[4]) {
small5.push(n);
small5.sort((a, b) => a - b); // Ensure the smallest numbers are sorted
if (small5.length > 5) small5.pop(); // Keep only the smallest 5
}
// Maintain 5 largest numbers
if (big5.length < 5 || n > big5[4]) {
big5.push(n);
big5.sort((a, b) => b - a); // Ensure the largest numbers are sorted
if (big5.length > 5) big5.pop(); // Keep only the largest 5
}
}
// Calculate the total sum
const total = small5.reduce((sum, num) => sum + num, 0) +
big5.reduce((sum, num) => sum + num, 0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Me | |
Chatgpt |
Test name | Executions per second |
---|---|
Me | 707017.1 Ops/sec |
Chatgpt | 221905.9 Ops/sec |
In this benchmark, we are comparing two different JavaScript implementations aimed at calculating the sum of the five smallest and five largest numbers in an array of integers. The two implementations are labeled as "Me" and "Chatgpt".
Test Case: "Me"
arr
, which contains a set of predefined integers. It maintains two arrays (small5
and big5
) for the smallest and largest five numbers, respectively.small5
and big5
with the first element of arr
.arr
, it checks if the number should be included in the five smallest (small5
) or largest (big5
) arrays and inserts it in sorted order using splice
.Test Case: "Chatgpt"
small5
and big5
.small5
and big5
as empty arrays.arr
, it pushes the number if it should belong to either small5
or big5
, then sorts the arrays and pops the last element if they exceed five elements.small5
and big5
."Me" Implementation:
splice
may introduce overhead, as it has a linear time complexity relative to the number of elements in the array when inserting."Chatgpt" Implementation:
Array.sort()
, which is a common operation in JavaScript.The benchmark results show that the "Me" test case achieves an execution rate of approximately 1,478,177 operations per second, while the "Chatgpt" implementation has a performance of about 453,713 operations per second. This indicates that the approach taken in the "Me" implementation is substantially faster than the one used in "Chatgpt".
There are several other approaches that could be considered for this problem:
By understanding these different implementations and their trade-offs, developers can make better-informed decisions on how to approach similar problems in their own work.