es-toolkit/benchmarks/performance/orderBy.bench.ts
Dayong Lee 3eb93e531c
Some checks failed
CI / codecov (push) Has been cancelled
Release / release (push) Has been cancelled
feat(orderBy, sortBy): support custom key functions in orderBy and integrate sortBy with orderBy (#407)
2024-08-22 23:28:27 +09:00

50 lines
1.6 KiB
TypeScript

import { bench, describe } from 'vitest';
import { orderBy as orderByToolkit } from 'es-toolkit';
import { orderBy as orderByToolkitCompat } from 'es-toolkit/compat';
import { orderBy as orderByLodash } from 'lodash';
const users = [
{ user: 'fred', age: 48, nested: { user: 'fred' } },
{ user: 'barney', age: 34, nested: { user: 'barney' } },
{ user: 'fred', age: 40, nested: { user: 'fred' } },
{ user: 'barney', age: 36, nested: { user: 'bar' } },
];
const keys: Array<keyof (typeof users)[0]> = ['user', 'age'];
const orders: Array<'asc' | 'desc'> = ['asc', 'desc'];
describe('orderBy', () => {
bench('es-toolkit/orderBy', () => {
orderByToolkit(users, keys, orders);
orderByToolkit(users, [user => user.user, user => user.age], orders);
});
bench('es-toolkit/compat/orderBy', () => {
orderByToolkitCompat(users, keys, orders);
orderByToolkitCompat(users, [user => user.user, user => user.age], orders);
});
bench('lodash/orderBy', () => {
orderByLodash(users, keys, orders);
orderByLodash(users, [user => user.user, user => user.age], orders);
});
});
describe('orderBy (nested property names)', () => {
bench('es-toolkit/compat/orderBy', () => {
orderByToolkitCompat(users, [['nested', 'user'], ['age']], orders);
});
bench('lodash/orderBy', () => {
orderByLodash(users, [['nested', 'user'], ['age']], orders);
});
});
describe('orderBy (property path)', () => {
bench('es-toolkit/compat/orderBy', () => {
orderByToolkitCompat(users, ['nested.user', 'age'], orders);
});
bench('lodash/orderBy', () => {
orderByLodash(users, ['nested.user', 'age'], orders);
});
});