You've already forked joplin
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:
21
packages/server/src/utils/array.test.ts
Normal file
21
packages/server/src/utils/array.test.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
||||||
export function unique(array: any[]): any[] {
|
export function unique(array: any[]): any[] {
|
||||||
return array.filter((elem, index, self) => {
|
if (array.length < 100) { // O(n^2) branch, but doesn't require creating a Set
|
||||||
return index === self.indexOf(elem);
|
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 => {
|
export const randomElement = <T>(array: T[]): T => {
|
||||||
|
|||||||
Reference in New Issue
Block a user