Skip to content

Commit 9ef52d7

Browse files
committed
perf(Math): Avoid Math.min, Math.max in vtkMath.arrayMin, vtkMath.arrayMax
Timings: Existing Math.min: ~210ms for arrayMin, ~2712ms for entire volume rendering initialization Comparison operator: ~35ms for arrayMin, ~2070ms for entire volume rendering initialization Ternary operator: ~37ms for arrayMin, ~2125ms for entire volume rendering initialization
1 parent 5c77a5a commit 9ef52d7

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

Sources/Common/Core/Math/index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const { vtkErrorMacro, vtkWarningMacro } = macro;
1212
let randomSeedValue = 0;
1313
const VTK_MAX_ROTATIONS = 20;
1414
const VTK_SMALL_NUMBER = 1.0e-12;
15-
const MAX_FUNCTION_ARGUMENTS = 32768;
1615

1716
function notImplemented(method) {
1817
return () => vtkErrorMacro(`vtkMath::${method} - NOT IMPLEMENTED`);
@@ -45,25 +44,21 @@ const { round, floor, ceil, min, max } = Math;
4544

4645
function arrayMin(arr) {
4746
let minValue = Infinity;
48-
for (let i = 0, len = arr.length; i < len; i += MAX_FUNCTION_ARGUMENTS) {
49-
const submin = Math.min.apply(
50-
null,
51-
arr.slice(i, Math.min(i + MAX_FUNCTION_ARGUMENTS, len))
52-
);
53-
minValue = Math.min(submin, minValue);
47+
for (let i = 0, len = arr.length; i < len; ++i) {
48+
if (arr[i] < minValue) {
49+
minValue = arr[i];
50+
}
5451
}
5552

5653
return minValue;
5754
}
5855

5956
function arrayMax(arr) {
6057
let maxValue = -Infinity;
61-
for (let i = 0, len = arr.length; i < len; i += MAX_FUNCTION_ARGUMENTS) {
62-
const submax = Math.max.apply(
63-
null,
64-
arr.slice(i, Math.min(i + MAX_FUNCTION_ARGUMENTS, len))
65-
);
66-
maxValue = Math.max(submax, maxValue);
58+
for (let i = 0, len = arr.length; i < len; ++i) {
59+
if (arr[i] > maxValue) {
60+
maxValue = arr[i];
61+
}
6762
}
6863

6964
return maxValue;

0 commit comments

Comments
 (0)