test: Add benchmarks for bundle size

This commit is contained in:
raon0211 2024-07-17 11:47:39 +09:00
parent aaf41e4966
commit c763c8c51f
81 changed files with 364 additions and 6 deletions

View File

@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest"
import { getBundleSize } from "./utils/getBundleSize"
describe('camelCase bundle size', () => {
it('lodash-es', async () => {
const bundleSize = await getBundleSize('lodash-es', 'camelCase');
expect(bundleSize).toMatchInlineSnapshot(`7293`);
});
it('es-toolkit', async () => {
const bundleSize = await getBundleSize('es-toolkit', 'camelCase');
expect(bundleSize).toMatchInlineSnapshot(`297`);
})
});

View File

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

View File

@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest"
import { getBundleSize } from "./utils/getBundleSize"
describe('difference bundle size', () => {
it('lodash-es', async () => {
const bundleSize = await getBundleSize('lodash-es', 'difference');
expect(bundleSize).toMatchInlineSnapshot(`7958`);
});
it('es-toolkit', async () => {
const bundleSize = await getBundleSize('es-toolkit', 'difference');
expect(bundleSize).toMatchInlineSnapshot(`90`);
})
});

View File

@ -0,0 +1,20 @@
import esbuild from 'esbuild';
import path from 'path';
export async function getBundleSize(pkg: 'lodash-es' | 'es-toolkit' | 'es-toolkit/compat', funcName: string) {
const script = `import { ${funcName} } from "${pkg}"; console.log(${funcName})`;
const bundled = await esbuild.build({
stdin: {
contents: script,
resolveDir: import.meta.dirname,
sourcefile: path.resolve(import.meta.dirname, 'test.js'),
loader: 'js',
},
write: false,
minify: true,
bundle: true,
});
return Buffer.from(bundled.outputFiles![0].contents).byteLength;
}

View File

@ -1,12 +1,18 @@
{
"name": "benchmarks",
"private": true,
"scripts": {
"check-bundle-size": "vitest --update"
},
"dependencies": {
"es-toolkit": "workspace:^",
"esbuild": "0.23.0",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"vitest": "^2.0.2"
},
"devDependencies": {
"@types/lodash": "^4"
"@types/lodash": "^4",
"@types/lodash-es": "^4"
}
}
}

View File

@ -1,13 +1,18 @@
import { bench, describe } from 'vitest';
import { chunk as chunkToolkit } from 'es-toolkit';
import { chunk as chunkCompatToolkit } from 'es-toolkit/compat';
import { chunk as chunkLodash } from 'lodash';
describe('chunk', () => {
bench('lodash/chunk', () => {
chunkLodash([1, 2, 3, 4, 5, 6], 3);
});
bench('es-toolkit/chunk', () => {
chunkToolkit([1, 2, 3, 4, 5, 6], 3);
});
bench('lodash/chunk', () => {
chunkLodash([1, 2, 3, 4, 5, 6], 3);
bench('es-toolkit/compat/chunk', () => {
chunkCompatToolkit([1, 2, 3, 4, 5, 6], 3);
});
});

View File

@ -18,6 +18,9 @@ describe('clone', () => {
clone(obj);
});
bench('lodash/clone', () => {
lodashClone(obj);
class Foo {
foo: () => void;
}
lodashClone(new Foo());
});
});

View File

@ -150,4 +150,4 @@
"lint": "eslint ./src --ext .ts",
"format": "prettier --write ."
}
}
}

277
yarn.lock
View File

@ -2087,6 +2087,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/aix-ppc64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/aix-ppc64@npm:0.23.0"
conditions: os=aix & cpu=ppc64
languageName: node
linkType: hard
"@esbuild/android-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/android-arm64@npm:0.20.2"
@ -2101,6 +2108,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/android-arm64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/android-arm64@npm:0.23.0"
conditions: os=android & cpu=arm64
languageName: node
linkType: hard
"@esbuild/android-arm@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/android-arm@npm:0.20.2"
@ -2115,6 +2129,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/android-arm@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/android-arm@npm:0.23.0"
conditions: os=android & cpu=arm
languageName: node
linkType: hard
"@esbuild/android-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/android-x64@npm:0.20.2"
@ -2129,6 +2150,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/android-x64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/android-x64@npm:0.23.0"
conditions: os=android & cpu=x64
languageName: node
linkType: hard
"@esbuild/darwin-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/darwin-arm64@npm:0.20.2"
@ -2143,6 +2171,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/darwin-arm64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/darwin-arm64@npm:0.23.0"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard
"@esbuild/darwin-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/darwin-x64@npm:0.20.2"
@ -2157,6 +2192,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/darwin-x64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/darwin-x64@npm:0.23.0"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard
"@esbuild/freebsd-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/freebsd-arm64@npm:0.20.2"
@ -2171,6 +2213,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/freebsd-arm64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/freebsd-arm64@npm:0.23.0"
conditions: os=freebsd & cpu=arm64
languageName: node
linkType: hard
"@esbuild/freebsd-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/freebsd-x64@npm:0.20.2"
@ -2185,6 +2234,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/freebsd-x64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/freebsd-x64@npm:0.23.0"
conditions: os=freebsd & cpu=x64
languageName: node
linkType: hard
"@esbuild/linux-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-arm64@npm:0.20.2"
@ -2199,6 +2255,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-arm64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-arm64@npm:0.23.0"
conditions: os=linux & cpu=arm64
languageName: node
linkType: hard
"@esbuild/linux-arm@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-arm@npm:0.20.2"
@ -2213,6 +2276,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-arm@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-arm@npm:0.23.0"
conditions: os=linux & cpu=arm
languageName: node
linkType: hard
"@esbuild/linux-ia32@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-ia32@npm:0.20.2"
@ -2227,6 +2297,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-ia32@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-ia32@npm:0.23.0"
conditions: os=linux & cpu=ia32
languageName: node
linkType: hard
"@esbuild/linux-loong64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-loong64@npm:0.20.2"
@ -2241,6 +2318,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-loong64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-loong64@npm:0.23.0"
conditions: os=linux & cpu=loong64
languageName: node
linkType: hard
"@esbuild/linux-mips64el@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-mips64el@npm:0.20.2"
@ -2255,6 +2339,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-mips64el@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-mips64el@npm:0.23.0"
conditions: os=linux & cpu=mips64el
languageName: node
linkType: hard
"@esbuild/linux-ppc64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-ppc64@npm:0.20.2"
@ -2269,6 +2360,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-ppc64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-ppc64@npm:0.23.0"
conditions: os=linux & cpu=ppc64
languageName: node
linkType: hard
"@esbuild/linux-riscv64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-riscv64@npm:0.20.2"
@ -2283,6 +2381,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-riscv64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-riscv64@npm:0.23.0"
conditions: os=linux & cpu=riscv64
languageName: node
linkType: hard
"@esbuild/linux-s390x@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-s390x@npm:0.20.2"
@ -2297,6 +2402,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-s390x@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-s390x@npm:0.23.0"
conditions: os=linux & cpu=s390x
languageName: node
linkType: hard
"@esbuild/linux-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-x64@npm:0.20.2"
@ -2311,6 +2423,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-x64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/linux-x64@npm:0.23.0"
conditions: os=linux & cpu=x64
languageName: node
linkType: hard
"@esbuild/netbsd-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/netbsd-x64@npm:0.20.2"
@ -2325,6 +2444,20 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/netbsd-x64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/netbsd-x64@npm:0.23.0"
conditions: os=netbsd & cpu=x64
languageName: node
linkType: hard
"@esbuild/openbsd-arm64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/openbsd-arm64@npm:0.23.0"
conditions: os=openbsd & cpu=arm64
languageName: node
linkType: hard
"@esbuild/openbsd-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/openbsd-x64@npm:0.20.2"
@ -2339,6 +2472,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/openbsd-x64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/openbsd-x64@npm:0.23.0"
conditions: os=openbsd & cpu=x64
languageName: node
linkType: hard
"@esbuild/sunos-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/sunos-x64@npm:0.20.2"
@ -2353,6 +2493,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/sunos-x64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/sunos-x64@npm:0.23.0"
conditions: os=sunos & cpu=x64
languageName: node
linkType: hard
"@esbuild/win32-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/win32-arm64@npm:0.20.2"
@ -2367,6 +2514,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/win32-arm64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/win32-arm64@npm:0.23.0"
conditions: os=win32 & cpu=arm64
languageName: node
linkType: hard
"@esbuild/win32-ia32@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/win32-ia32@npm:0.20.2"
@ -2381,6 +2535,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/win32-ia32@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/win32-ia32@npm:0.23.0"
conditions: os=win32 & cpu=ia32
languageName: node
linkType: hard
"@esbuild/win32-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/win32-x64@npm:0.20.2"
@ -2395,6 +2556,13 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/win32-x64@npm:0.23.0":
version: 0.23.0
resolution: "@esbuild/win32-x64@npm:0.23.0"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard
"@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0":
version: 4.4.0
resolution: "@eslint-community/eslint-utils@npm:4.4.0"
@ -2956,6 +3124,22 @@ __metadata:
languageName: node
linkType: hard
"@types/lodash-es@npm:^4":
version: 4.17.12
resolution: "@types/lodash-es@npm:4.17.12"
dependencies:
"@types/lodash": "npm:*"
checksum: 10c0/5d12d2cede07f07ab067541371ed1b838a33edb3c35cb81b73284e93c6fd0c4bbeaefee984e69294bffb53f62d7272c5d679fdba8e595ff71e11d00f2601dde0
languageName: node
linkType: hard
"@types/lodash@npm:*":
version: 4.17.7
resolution: "@types/lodash@npm:4.17.7"
checksum: 10c0/40c965b5ffdcf7ff5c9105307ee08b782da228c01b5c0529122c554c64f6b7168fc8f11dc79aa7bae4e67e17efafaba685dc3a47e294dbf52a65ed2b67100561
languageName: node
linkType: hard
"@types/lodash@npm:^4":
version: 4.17.4
resolution: "@types/lodash@npm:4.17.4"
@ -3908,8 +4092,11 @@ __metadata:
resolution: "benchmarks@workspace:benchmarks"
dependencies:
"@types/lodash": "npm:^4"
"@types/lodash-es": "npm:^4"
es-toolkit: "workspace:^"
esbuild: "npm:^0.23.0"
lodash: "npm:^4.17.21"
lodash-es: "npm:^4.17.21"
vitest: "npm:^2.0.2"
languageName: unknown
linkType: soft
@ -5206,6 +5393,89 @@ __metadata:
languageName: node
linkType: hard
"esbuild@npm:^0.23.0":
version: 0.23.0
resolution: "esbuild@npm:0.23.0"
dependencies:
"@esbuild/aix-ppc64": "npm:0.23.0"
"@esbuild/android-arm": "npm:0.23.0"
"@esbuild/android-arm64": "npm:0.23.0"
"@esbuild/android-x64": "npm:0.23.0"
"@esbuild/darwin-arm64": "npm:0.23.0"
"@esbuild/darwin-x64": "npm:0.23.0"
"@esbuild/freebsd-arm64": "npm:0.23.0"
"@esbuild/freebsd-x64": "npm:0.23.0"
"@esbuild/linux-arm": "npm:0.23.0"
"@esbuild/linux-arm64": "npm:0.23.0"
"@esbuild/linux-ia32": "npm:0.23.0"
"@esbuild/linux-loong64": "npm:0.23.0"
"@esbuild/linux-mips64el": "npm:0.23.0"
"@esbuild/linux-ppc64": "npm:0.23.0"
"@esbuild/linux-riscv64": "npm:0.23.0"
"@esbuild/linux-s390x": "npm:0.23.0"
"@esbuild/linux-x64": "npm:0.23.0"
"@esbuild/netbsd-x64": "npm:0.23.0"
"@esbuild/openbsd-arm64": "npm:0.23.0"
"@esbuild/openbsd-x64": "npm:0.23.0"
"@esbuild/sunos-x64": "npm:0.23.0"
"@esbuild/win32-arm64": "npm:0.23.0"
"@esbuild/win32-ia32": "npm:0.23.0"
"@esbuild/win32-x64": "npm:0.23.0"
dependenciesMeta:
"@esbuild/aix-ppc64":
optional: true
"@esbuild/android-arm":
optional: true
"@esbuild/android-arm64":
optional: true
"@esbuild/android-x64":
optional: true
"@esbuild/darwin-arm64":
optional: true
"@esbuild/darwin-x64":
optional: true
"@esbuild/freebsd-arm64":
optional: true
"@esbuild/freebsd-x64":
optional: true
"@esbuild/linux-arm":
optional: true
"@esbuild/linux-arm64":
optional: true
"@esbuild/linux-ia32":
optional: true
"@esbuild/linux-loong64":
optional: true
"@esbuild/linux-mips64el":
optional: true
"@esbuild/linux-ppc64":
optional: true
"@esbuild/linux-riscv64":
optional: true
"@esbuild/linux-s390x":
optional: true
"@esbuild/linux-x64":
optional: true
"@esbuild/netbsd-x64":
optional: true
"@esbuild/openbsd-arm64":
optional: true
"@esbuild/openbsd-x64":
optional: true
"@esbuild/sunos-x64":
optional: true
"@esbuild/win32-arm64":
optional: true
"@esbuild/win32-ia32":
optional: true
"@esbuild/win32-x64":
optional: true
bin:
esbuild: bin/esbuild
checksum: 10c0/08c148c067795165798c0467ce02d2d1ecedc096989bded5f0d795c61a1fcbec6c14d0a3c9f4ad6185cc29ec52087acaa335ed6d98be6ad57f7fa4264626bde0
languageName: node
linkType: hard
"escalade@npm:^3.1.1":
version: 3.1.2
resolution: "escalade@npm:3.1.2"
@ -6869,6 +7139,13 @@ __metadata:
languageName: node
linkType: hard
"lodash-es@npm:^4.17.21":
version: 4.17.21
resolution: "lodash-es@npm:4.17.21"
checksum: 10c0/fb407355f7e6cd523a9383e76e6b455321f0f153a6c9625e21a8827d10c54c2a2341bd2ae8d034358b60e07325e1330c14c224ff582d04612a46a4f0479ff2f2
languageName: node
linkType: hard
"lodash.clonedeep@npm:^4.5.0":
version: 4.5.0
resolution: "lodash.clonedeep@npm:4.5.0"