2024-07-12 03:56:21 +03:00
|
|
|
import { bench, describe } from 'vitest';
|
|
|
|
import { flattenDeep as flattenDeepToolkit } from 'es-toolkit';
|
2024-08-02 16:00:24 +03:00
|
|
|
import { flattenDeep as flattenDeepToolkitCompat } from 'es-toolkit/compat';
|
2024-07-12 03:56:21 +03:00
|
|
|
import { flattenDeep as flattenDeepLodash } from 'lodash';
|
|
|
|
|
|
|
|
const createNestedArray = (values: number[]) => {
|
|
|
|
if (values.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const [first, ...rest] = values;
|
|
|
|
return [first, createNestedArray(rest)];
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('flattenDeep', () => {
|
|
|
|
const arr = createNestedArray(Array.from({ length: 30 }, (_, index) => index));
|
|
|
|
|
|
|
|
bench('es-toolkit/flattenDeep', () => {
|
|
|
|
flattenDeepToolkit(arr);
|
|
|
|
});
|
|
|
|
|
2024-08-02 16:00:24 +03:00
|
|
|
bench('es-toolkit/flattenDeep (compat)', () => {
|
|
|
|
flattenDeepToolkitCompat(arr);
|
|
|
|
});
|
|
|
|
|
2024-07-12 03:56:21 +03:00
|
|
|
bench('lodash/flattenDeep', () => {
|
|
|
|
flattenDeepLodash(arr);
|
|
|
|
});
|
|
|
|
|
|
|
|
bench('js built-in/flat(Infinity)', () => {
|
|
|
|
arr.flat(Infinity);
|
|
|
|
});
|
|
|
|
});
|