function genArray(shape) {
var random = function () { return Math.random(); };
var arr = [];
for (var i = 0; i < 1; i++) {
arr.push(genShape(shape, random()));
}
return arr;
}
function genShape(shape, number) {
shape = shape % 10;
var obj = {};
for (var i = 0; i < shape; i++) {
obj["foo" + i] = "bar";
}
obj.a = number;
for (i = shape; i <= 10; i++) {
obj["bar" + i] = "foo";
}
return obj;
}
var arr0 = genArray(0);
var arr1 = genArray(1);
var arr2 = genArray(2);
var arr3 = genArray(3);
var arr4 = genArray(4);
var arr5 = genArray(5);
var arr6 = genArray(6);
var arr7 = genArray(7);
var arr8 = genArray(8);
var arr9 = genArray(9);
function doOverloadedTest() {
var res = 0;
res += sum(arr0);
res += sum(arr1);
res += sum(arr2);
res += sum(arr3);
res += sum(arr4);
res += sum(arr5);
res += sum(arr6);
res += sum(arr7);
res += sum(arr8);
res += sum(arr9);
return res;
}
function sum(arr) {
var res = 0;
for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
}
return res;
}
function sum0(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum1(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum2(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum3(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum4(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum5(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum6(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum7(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum8(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function sum9(arr) { var res = 0; for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
} return res; }
function doSplitTest() {
var res = 0;
res += sum0(arr0);
res += sum1(arr1);
res += sum2(arr2);
res += sum3(arr3);
res += sum4(arr4);
res += sum5(arr5);
res += sum6(arr6);
res += sum7(arr7);
res += sum8(arr8);
res += sum9(arr9);
return res;
}
var sumSplit = (function () {
var res = function sum(arr) {
var res = 0;
for (var i = 0; i < arr.length; i++) {
res += arr[i].a;
}
return res;
};
res.sum0 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum1 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum2 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum3 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum4 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum5 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum6 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum7 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum8 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
res.sum9 = function sum(arr) { var res = 0; for (var i = 0; i < arr.length; i++) { res += arr[i].a; } return res; };
return res;
})();
function doAutoSplitTest() {
var res = 0;
res += ((sumSplit && sumSplit.sum0) || sumSplit)(arr0);
res += ((sumSplit && sumSplit.sum1) || sumSplit)(arr1);
res += ((sumSplit && sumSplit.sum2) || sumSplit)(arr2);
res += ((sumSplit && sumSplit.sum3) || sumSplit)(arr3);
res += ((sumSplit && sumSplit.sum4) || sumSplit)(arr4);
res += ((sumSplit && sumSplit.sum5) || sumSplit)(arr5);
res += ((sumSplit && sumSplit.sum6) || sumSplit)(arr6);
res += ((sumSplit && sumSplit.sum7) || sumSplit)(arr7);
res += ((sumSplit && sumSplit.sum8) || sumSplit)(arr8);
res += ((sumSplit && sumSplit.sum9) || sumSplit)(arr9);
return res;
}
window.doOverloadedTest = doOverloadedTest;
window.doSplitTest = doSplitTest;
window.doAutoSplitTest = doAutoSplitTest;
window.doOverloadedTest()
window.doSplitTest()
window.doAutoSplitTest()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Overloaded sum test | |
Split sum test | |
Auto split test |
Test name | Executions per second |
---|---|
Overloaded sum test | 356320.9 Ops/sec |
Split sum test | 321945.5 Ops/sec |
Auto split test | 273571.9 Ops/sec |
It appears that the provided code is a benchmarking script, likely used to measure the performance of JavaScript functions. The script defines several functions (sum0
, sum1
, etc.) and benchmarks their execution times using various test cases.
The functions seem to be designed to sum up elements in an array while incrementing a counter for each element processed. However, upon closer inspection, it appears that these functions are not actually being used in the benchmarking script.
Instead, the script uses the doOverloadedTest
, doSplitTest
, and doAutoSplitTest
functions to benchmark the execution times of different versions of the same function, which is defined as sum
. The sum
function takes an array as input and returns the sum of its elements.
The latest benchmark result shows that:
These results suggest that the "Auto split test" is currently the fastest among the three tests. However, without more context or information about the specific implementation of these functions and how they are optimized for different use cases (split or auto), it's difficult to provide a definitive answer on why this might be the case.