docs: Add benchmarks to some of our functions

This commit is contained in:
Sojin Park 2024-08-10 16:58:00 +09:00
parent b7a55063ab
commit 1022ea52a6
19 changed files with 223 additions and 16 deletions

View File

@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { getBundleSize } from './utils/getBundleSize';
describe('cloneDeep bundle size', () => {
it('lodash-es', async () => {
const bundleSize = await getBundleSize('lodash-es', 'cloneDeep');
expect(bundleSize).toMatchInlineSnapshot(`14703`);
});
it('es-toolkit', async () => {
const bundleSize = await getBundleSize('es-toolkit', 'cloneDeep');
expect(bundleSize).toMatchInlineSnapshot(`2048`);
});
it('es-toolkit/compat', async () => {
const bundleSize = await getBundleSize('es-toolkit/compat', 'cloneDeep');
expect(bundleSize).toMatchInlineSnapshot(`2048`);
});
});

View File

@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { getBundleSize } from './utils/getBundleSize';
describe('isPlainObject bundle size', () => {
it('lodash-es', async () => {
const bundleSize = await getBundleSize('lodash-es', 'isPlainObject');
expect(bundleSize).toMatchInlineSnapshot(`1586`);
});
it('es-toolkit', async () => {
const bundleSize = await getBundleSize('es-toolkit', 'isPlainObject');
expect(bundleSize).toMatchInlineSnapshot(`279`);
});
it('es-toolkit/compat', async () => {
const bundleSize = await getBundleSize('es-toolkit/compat', 'isPlainObject');
expect(bundleSize).toMatchInlineSnapshot(`435`);
});
});

View File

@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { getBundleSize } from './utils/getBundleSize';
describe('mapKeys bundle size', () => {
it('lodash-es', async () => {
const bundleSize = await getBundleSize('lodash-es', 'mapKeys');
expect(bundleSize).toMatchInlineSnapshot(`16649`);
});
it('es-toolkit', async () => {
const bundleSize = await getBundleSize('es-toolkit', 'mapKeys');
expect(bundleSize).toMatchInlineSnapshot(`138`);
});
it('es-toolkit/compat', async () => {
const bundleSize = await getBundleSize('es-toolkit/compat', 'mapKeys');
expect(bundleSize).toMatchInlineSnapshot(`1124`);
});
});

View File

@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { getBundleSize } from './utils/getBundleSize';
describe('mapValues bundle size', () => {
it('lodash-es', async () => {
const bundleSize = await getBundleSize('lodash-es', 'mapValues');
expect(bundleSize).toMatchInlineSnapshot(`16649`);
});
it('es-toolkit', async () => {
const bundleSize = await getBundleSize('es-toolkit', 'mapValues');
expect(bundleSize).toMatchInlineSnapshot(`138`);
});
it('es-toolkit/compat', async () => {
const bundleSize = await getBundleSize('es-toolkit/compat', 'mapValues');
expect(bundleSize).toMatchInlineSnapshot(`1124`);
});
});

View File

@ -1,19 +1,19 @@
import { describe, expect, it } from 'vitest';
import { getBundleSize } from './utils/getBundleSize';
describe('camelCase bundle size', () => {
describe('merge bundle size', () => {
it('lodash-es', async () => {
const bundleSize = await getBundleSize('lodash-es', 'camelCase');
expect(bundleSize).toMatchInlineSnapshot(`7293`);
const bundleSize = await getBundleSize('lodash-es', 'merge');
expect(bundleSize).toMatchInlineSnapshot(`12483`);
});
it('es-toolkit', async () => {
const bundleSize = await getBundleSize('es-toolkit', 'merge');
expect(bundleSize).toMatchInlineSnapshot(`252`);
expect(bundleSize).toMatchInlineSnapshot(`271`);
});
it('es-toolkit/compat', async () => {
const bundleSize = await getBundleSize('es-toolkit/compat', 'merge');
expect(bundleSize).toMatchInlineSnapshot(`4256`);
expect(bundleSize).toMatchInlineSnapshot(`4381`);
});
});

View File

@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { getBundleSize } from './utils/getBundleSize';
describe('mergeWith bundle size', () => {
it('lodash-es', async () => {
const bundleSize = await getBundleSize('lodash-es', 'mergeWith');
expect(bundleSize).toMatchInlineSnapshot(`12487`);
});
it('es-toolkit', async () => {
const bundleSize = await getBundleSize('es-toolkit', 'mergeWith');
expect(bundleSize).toMatchInlineSnapshot(`307`);
});
it('es-toolkit/compat', async () => {
const bundleSize = await getBundleSize('es-toolkit/compat', 'mergeWith');
expect(bundleSize).toMatchInlineSnapshot(`4329`);
});
});

View File

@ -0,0 +1,33 @@
import { bench, describe } from 'vitest';
import { isPlainObject as isPlainObjectToolkit } from 'es-toolkit';
import { isPlainObject as isPlainObjectCompatToolkit } from 'es-toolkit/compat';
import { isPlainObject as isPlainObjectLodash } from 'lodash';
const object = Object.create(null);
const buffer = Buffer.from('hello, world');
describe('isPlainObject', () => {
bench('es-toolkit/isPlainObject', () => {
isPlainObjectToolkit({});
isPlainObjectToolkit([]);
isPlainObjectToolkit(null);
isPlainObjectToolkit(object);
isPlainObjectToolkit(buffer);
});
bench('es-toolkit/compat/isPlainObject', () => {
isPlainObjectCompatToolkit({});
isPlainObjectCompatToolkit([]);
isPlainObjectCompatToolkit(null);
isPlainObjectCompatToolkit(object);
isPlainObjectCompatToolkit(buffer);
});
bench('lodash/isPlainObject', () => {
isPlainObjectLodash({});
isPlainObjectLodash([]);
isPlainObjectLodash(null);
isPlainObjectLodash(object);
isPlainObjectLodash(buffer);
});
});

View File

@ -1,5 +1,6 @@
import { bench, describe } from 'vitest';
import { mapKeys as mapKeysToolkit } from 'es-toolkit';
import { mapKeys as mapKeysCompatToolkit } from 'es-toolkit/compat';
import { mapKeys as mapKeysLodash } from 'lodash';
describe('mapKeys', () => {
@ -7,6 +8,10 @@ describe('mapKeys', () => {
mapKeysToolkit({ a: 1, b: 2, c: 3 }, (_value, key) => `${key}a`);
});
bench('es-toolkit/compat/mapKeys', () => {
mapKeysCompatToolkit({ a: 1, b: 2, c: 3 }, (_value, key) => `${key}a`);
});
bench('lodash/mapKeys', () => {
mapKeysLodash({ a: 1, b: 2, c: 3 }, (_value, key) => `${key}a`);
});

View File

@ -27,3 +27,11 @@ const obj = { a: 1, b: 2 };
const result = mapKeys(obj, (value, key) => key + value);
console.log(result); // { a1: 1, b2: 2 }
```
## 성능 비교
| | [번들 사이즈](../../bundle-size.md) | [런타임 성능](../../performance.md) |
| ----------------- | ----------------------------------- | ----------------------------------- |
| es-toolkit | 138 바이트 (99.1% 작음) | 2,844,013 회 (11% 빠름) |
| es-toolkit/compat | 1,124 바이트 (93.2% 작음) | 2.899,524 회 (13% 빠름) |
| lodash-es | 16,649 바이트 | 2,559,949 회 |

View File

@ -60,3 +60,11 @@ console.log(result);
```
:::
## 성능 비교
| | [번들 사이즈](../../bundle-size.md) | [런타임 성능](../../performance.md) |
| ----------------- | ----------------------------------- | ----------------------------------- |
| es-toolkit | 271 바이트 (97.8% 작음) | 1,952,436 회 (3.65× 빠름) |
| es-toolkit/compat | 4,381 바이트 (64.9% 작음) | 706,558 회 (1.32× 빠름) |
| lodash-es | 12,483 바이트 | 533,484 회 |

View File

@ -25,3 +25,11 @@ console.log(isPlainObject(null)); // false
console.log(isPlainObject(Object.create(null))); // true
console.log(Buffer.from('hello, world')); // false
```
## 성능 비교
| | [번들 사이즈](../../bundle-size.md) | [런타임 성능](../../performance.md) |
| ----------------- | ----------------------------------- | ----------------------------------- |
| es-toolkit | 279 바이트 (82.4% 작음) | 1,505,684 회 (1.70× 빠름) |
| es-toolkit/compat | 435 바이트 (72.5% 작음) | 2,013,760 회 (2.28× 빠름) |
| lodash-es | 1,586 바이트 | 882,669 회 |

View File

@ -27,3 +27,11 @@ const obj = { a: 1, b: 2 };
const result = mapKeys(obj, (value, key) => key + value);
console.log(result); // { a1: 1, b2: 2 }
```
## Performance Comparison
| | [Bundle Size](../../bundle-size.md) | [Performance](../../performance.md) |
| ----------------- | ----------------------------------- | ----------------------------------- |
| es-toolkit | 138 bytes (99.1% smaller) | 2,844,013 times (11% faster) |
| es-toolkit/compat | 1,124 bytes (93.2% smaller) | 2,899,524 times (13% faster) |
| lodash-es | 16,649 bytes | 2,559,949 times |

View File

@ -60,3 +60,11 @@ console.log(result);
```
:::
## Performance Comparison
| | [Bundle Size](../../bundle-size.md) | [Performance](../../performance.md) |
| ----------------- | ----------------------------------- | ----------------------------------- |
| es-toolkit | 271 bytes (97.8% smaller) | 1,952,436 times (3.65× faster) |
| es-toolkit/compat | 4,381 bytes (64.9% smaller) | 706,558 times (1.32× faster) |
| lodash-es | 12,483 bytes | 533,484 times |

View File

@ -25,3 +25,11 @@ console.log(isPlainObject(null)); // false
console.log(isPlainObject(Object.create(null))); // true
console.log(Buffer.from('hello, world')); // false
```
## Performance Comparison
| | [Bundle Size](../../bundle-size.md) | [Performance](../../performance.md) |
| ----------------- | ----------------------------------- | ----------------------------------- |
| es-toolkit | 279 bytes (82.4% smaller) | 1,505,684 times (1.70× faster) |
| es-toolkit/compat | 435 bytes (72.5% smaller) | 2,013,760 times (2.28× faster) |
| lodash-es | 1,586 bytes | 882,669 times |

View File

@ -34,7 +34,7 @@ const result = difference(array1, array2);
## 性能对比
| | [Bundle Size](../../bundle-size.md) | [Performance](../../performance.md) |
| ---------- | ----------------------------------- | ----------------------------------- |
| | [包大小](../../bundle-size.md) | [性能](../../performance.md) |
| ---------- | ------------------------------ | ---------------------------- |
| es-toolkit | 90 字节 (小 92.4%) | 9,317,227 次 (快 85%) |
| lodash-es | 7,958 字节 | 5,030,861 次 |

View File

@ -11,7 +11,7 @@
## 签名
```typescript
function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V }
function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V };
```
### 参数
@ -33,7 +33,10 @@ const values = [1, 2];
const result = zipObjectDeep(paths, values);
// 结果将是 { a: { b: { c: 1 } }, d: { e: { f: 2 } } }
const paths = [['a', 'b', 'c'], ['d', 'e', 'f']];
const paths = [
['a', 'b', 'c'],
['d', 'e', 'f'],
];
const values = [1, 2];
const result = zipObjectDeep(paths, values);
// 结果将是 { a: { b: { c: 1 } }, d: { e: { f: 2 } } }
@ -46,8 +49,7 @@ const result = zipObjectDeep(paths, values);
## 性能对比
| | [包大小](../../../bundle-size.md) | [运行时性能](../../../performance.md) |
| ----------------- | ------------------------------ | ---------------------------------- |
| | [包大小](../../bundle-size.md) | [性能](../../performance.md) |
| ----------------- | ------------------------------ | ---------------------------- |
| es-toolkit/compat | 938 字节 (小 88%) | 1,102,767 次 (慢 25%) |
| lodash-es | 7,338 字节 | 1,476,660 次 |

View File

@ -27,3 +27,11 @@ const obj = { a: 1, b: 2 };
const result = mapKeys(obj, (value, key) => key + value);
console.log(result); // { a1: 1, b2: 2 }
```
## 性能对比
| | [包大小](../../bundle-size.md) | [性能](../../performance.md) |
| ----------------- | ------------------------------ | ---------------------------- |
| es-toolkit | 138 字节 (小 99.1%) | 2,844,013 次 (快 11%) |
| es-toolkit/compat | 1,124 字节 (小 93.2%) | 2,899,524 次 (快 13%) |
| lodash-es | 16,649 字节 | 2,559,949 次 |

View File

@ -60,3 +60,11 @@ console.log(result);
```
:::
## 性能对比
| | [包大小](../../bundle-size.md) | [性能](../../performance.md) |
| ----------------- | ------------------------------ | ---------------------------- |
| es-toolkit | 271 字节 (小 92.4%) | 1,952,436 次 (快 3.65×) |
| es-toolkit/compat | 4,381 字节 (小 90.2%) | 706,558 次 (快 1.32×) |
| lodash-es | 12,483 字节 | 533,484 次 |

View File

@ -25,3 +25,11 @@ console.log(isPlainObject(null)); // false
console.log(isPlainObject(Object.create(null))); // true
console.log(Buffer.from('hello, world')); // false
```
## 性能对比
| | [包大小](../../bundle-size.md) | [性能](../../performance.md) |
| ----------------- | ------------------------------ | ---------------------------- |
| es-toolkit | 279 字节 (小 82.4%) | 1,505,684 次 (快 1.70×) |
| es-toolkit/compat | 435 字节 (小 72.5%) | 2,013,760 次 (快 2.28×) |
| lodash-es | 1,586 字节 | 882,669 次 |