1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-23 22:36:32 +02:00

Server: Performance: Improve performance of updating shared items, generating reports (#13674)

This commit is contained in:
Henry Heino
2025-11-11 14:49:24 -08:00
committed by GitHub
parent e626db3b8c
commit d1415a318c
2 changed files with 28 additions and 3 deletions

View File

@@ -0,0 +1,21 @@
import { unique } from './array';
describe('array', () => {
it('should return unique items in a short array', () => {
expect(unique([1, 2, 2, 3, 1])).toEqual([1, 2, 3]);
});
it('should return unique items in a large array', () => {
const array = [];
for (let j = 0; j < 2; j++) {
for (let i = 0; i < 1024; i++) {
array.push(i);
}
}
// Every item should be present twice
expect(array).toHaveLength(2048);
expect(unique(array)).toHaveLength(1024);
});
});

View File

@@ -1,8 +1,12 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
export function unique(array: any[]): any[] {
return array.filter((elem, index, self) => {
return index === self.indexOf(elem);
});
if (array.length < 100) { // O(n^2) branch, but doesn't require creating a Set
return array.filter((elem, index, self) => {
return index === self.indexOf(elem);
});
} else { // Faster than O(n^2)
return Array.from(new Set(array));
}
}
export const randomElement = <T>(array: T[]): T => {