mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-29 12:43:49 +03:00
2d1c406398
* feat(flatten): add compatibility * Add bench * Add doc * fix jsdoc
33 lines
823 B
TypeScript
33 lines
823 B
TypeScript
import { bench, describe } from 'vitest';
|
|
import { flatten as flattenToolkit } from 'es-toolkit';
|
|
import { flatten as flattenCompatToolkit } from 'es-toolkit/compat';
|
|
import { flattenDepth as flattenDepthLodash } from 'lodash';
|
|
|
|
const createNestedArray = (values: any[]) => {
|
|
if (values.length === 0) {
|
|
return [];
|
|
}
|
|
const [first, ...rest] = values;
|
|
return [first, createNestedArray(rest)];
|
|
};
|
|
|
|
describe('flatten', () => {
|
|
const arr = createNestedArray(Array.from({ length: 30 }, (_, index) => index));
|
|
|
|
bench('es-toolkit/flatten', () => {
|
|
flattenToolkit(arr, 30);
|
|
});
|
|
|
|
bench('es-toolkit/flatten (compat)', () => {
|
|
flattenCompatToolkit(arr, 30);
|
|
});
|
|
|
|
bench('lodash/flattenDepth', () => {
|
|
flattenDepthLodash(arr, 30);
|
|
});
|
|
|
|
bench('js built-in/flat', () => {
|
|
arr.flat(30);
|
|
});
|
|
});
|