Skip to content

Commit 81a7aef

Browse files
committed
fix: Maximum call stack size exceeded for large diffs
1 parent 5e8b247 commit 81a7aef

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

src/line-by-line-renderer.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
DiffLineContext,
1212
DiffLineInserted,
1313
} from './types';
14+
import { max } from './utils';
1415

1516
export interface LineByLineRendererConfig extends renderUtils.RenderConfig {
1617
renderNothingWhenEmpty?: boolean;
@@ -30,27 +31,6 @@ const baseTemplatesPath = 'line-by-line';
3031
const iconsBaseTemplatesPath = 'icon';
3132
const tagsBaseTemplatesPath = 'tag';
3233

33-
/**
34-
* @description Finds the maximum value in an array of numbers safely
35-
*
36-
* This function iterates through the array manually instead of using Math.max(...arr)
37-
* to avoid "Maximum call stack size exceeded" errors that can occur when spreading
38-
* very large arrays as function arguments. This is especially important when dealing
39-
* with large diffs that may contain thousands of lines.
40-
*
41-
* @param arr - The array of numbers
42-
* @returns The maximum value in the array
43-
*/
44-
function max(arr: number[]): number {
45-
const length = arr.length;
46-
let max = -Infinity;
47-
48-
for (let i = 0; i < length; i++) {
49-
max = Math.max(max, arr[i]);
50-
}
51-
return max;
52-
}
53-
5434
export default class LineByLineRenderer {
5535
private readonly hoganUtils: HoganJsUtils;
5636
private readonly config: typeof defaultLineByLineRendererConfig;

src/side-by-side-renderer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
DiffLineInserted,
1212
DiffLineContent,
1313
} from './types';
14+
import { max } from './utils';
1415

1516
export interface SideBySideRendererConfig extends renderUtils.RenderConfig {
1617
renderNothingWhenEmpty?: boolean;
@@ -194,10 +195,7 @@ export default class SideBySideRenderer {
194195
matcher: Rematch.MatcherFn<DiffLine>,
195196
): DiffLine[][][] {
196197
const comparisons = oldLines.length * newLines.length;
197-
const maxLineSizeInBlock = Math.max.apply(
198-
null,
199-
[0].concat(oldLines.concat(newLines).map(elem => elem.content.length)),
200-
);
198+
const maxLineSizeInBlock = max(oldLines.concat(newLines).map(elem => elem.content.length));
201199
const doMatching =
202200
comparisons < this.config.matchingMaxComparisons &&
203201
maxLineSizeInBlock < this.config.maxLineSizeInBlockForComparison &&

src/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,24 @@ export function hashCode(text: string): number {
5252

5353
return hash;
5454
}
55+
56+
/**
57+
* @description Finds the maximum value in an array of numbers safely
58+
*
59+
* This function iterates through the array manually instead of using Math.max(...arr)
60+
* to avoid "Maximum call stack size exceeded" errors that can occur when spreading
61+
* very large arrays as function arguments. This is especially important when dealing
62+
* with large diffs that may contain thousands of lines.
63+
*
64+
* @param arr - The array of numbers
65+
* @returns The maximum value in the array
66+
*/
67+
export function max(arr: number[]): number {
68+
const length = arr.length;
69+
let max = -Infinity;
70+
71+
for (let i = 0; i < length; i++) {
72+
max = Math.max(max, arr[i]);
73+
}
74+
return max;
75+
}

0 commit comments

Comments
 (0)