Skip to content

Commit 7f27cc6

Browse files
committed
use flat
1 parent d7f323f commit 7f27cc6

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

lib/index.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
5757
}
5858

5959
function createFP(slide: FarthestPoint, down: FarthestPoint, k: number, M: number, N: number): FarthestPoint {
60-
if (slide && slide.y === -1 && (down && down.y === -1)) return { y: 0, id: 0 };
60+
if (slide && slide.y === -1 && down && down.y === -1) return { y: 0, id: 0 };
6161
if ((down && down.y === -1) || k === M || (slide && slide.y) > (down && down.y) + 1) {
6262
const prev = slide.id;
6363
ptr++;
@@ -108,11 +108,11 @@ export default function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
108108
const offset = N;
109109
const delta = M - N;
110110
const size = M + N + 1;
111-
const fp = new Array(size).fill({ y: -1 });
111+
let fp = new Array(size).fill({ y: -1 });
112112
// INFO: This buffer is used to save memory and improve performance.
113113
// The first half is used to save route and last half is used to save diff type.
114114
// This is because, when I kept new uint8array area to save type, performance worsened.
115-
const routes = new Uint32Array((M * N + size + 1) * 2);
115+
let routes = new Uint32Array((M * N + size + 1) * 2);
116116
const diffTypesPtrOffset = routes.length / 2;
117117
let ptr = 0;
118118
let p = -1;
@@ -126,9 +126,17 @@ export default function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
126126
}
127127
fp[delta + offset] = snake(delta, fp[delta - 1 + offset], fp[delta + 1 + offset], offset, A, B);
128128
}
129-
return [
130-
...prefixCommon.map(c => ({ type: 'common' as DiffType, value: c })),
131-
...backTrace(A, B, fp[delta + offset], swapped),
132-
...suffixCommon.map(c => ({ type: 'common' as DiffType, value: c })),
133-
];
129+
const pre = prefixCommon.map(c => ({ type: 'common' as DiffType, value: c }));
130+
const traced = backTrace(A, B, fp[delta + offset], swapped);
131+
const suf = suffixCommon.map(c => ({ type: 'common' as DiffType, value: c }));
132+
133+
// cleanup
134+
(routes as any) = null;
135+
(fp as any) = null;
136+
137+
if ('flat' in Array.prototype) {
138+
return [pre, traced, suf].flat();
139+
} else {
140+
return [...pre, ...traced, ...suf].flat();
141+
}
134142
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"module": "commonjs",
1010
"lib": [
1111
"es2015",
12+
"esnext",
1213
"dom"
1314
],
1415
// "allowJs": true, /* Allow javascript files to be compiled. */

0 commit comments

Comments
 (0)