-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The import language affect the benchmark result #6543
Comments
The same issue arises when calling with the subpath as shown below, resulting in a performance difference. Beforeimport { bench, describe } from 'vitest';
import { after as afterToolkit } from 'es-toolkit'; // This is difference.
import { after as afterLodash } from 'lodash';
describe('after', () => {
bench('es-toolkit/after', () => {
const add = (a: number, b: number) => a + b;
const n = 10;
const afterFn = afterToolkit(n, add);
for (let i = 0; i < n + 1; i++) {
afterFn(1, 2);
}
});
bench('lodash/after', () => {
const add = (a: number, b: number) => a + b;
const n = 10;
const afterFn = afterLodash(n, add);
for (let i = 0; i < n + 1; i++) {
afterFn(1, 2);
}
});
}); Bench ResultUsing subpathimport { bench, describe } from 'vitest';
import { after as afterToolkit } from 'es-toolkit/function'; // This is difference.
import { after as afterLodash } from 'lodash';
describe('after', () => {
bench('es-toolkit/after', () => {
const add = (a: number, b: number) => a + b;
const n = 10;
const afterFn = afterToolkit(n, add);
for (let i = 0; i < n + 1; i++) {
afterFn(1, 2);
}
});
bench('lodash/after', () => {
const add = (a: number, b: number) => a + b;
const n = 10;
const afterFn = afterLodash(n, add);
for (let i = 0; i < n + 1; i++) {
afterFn(1, 2);
}
});
}); Bench ResultAdditionalYou can see our barrel files in here(src/index.ts, src/function/index.ts). Thanks |
This is likely due to how Vite/Vitest transform and import modules. You can roughly see that by For example, in this reproduction https://stackblitz.com/edit/vitest-dev-vitest-br64n3?file=src%2Fbasic.bench.ts, // basic.ts
export const squared = (n: number) => n * n
// basic.bench.ts
import { squared } from './basic.js';
squared(2) will be transformed to something like const viteSsrExports = Object.create(null);
const viteSsrExportsSquared = (n: number) => n * n;
Object.defineProperty(viteSsrExports, 'squared', {
enumerable: true,
configurable: true,
get() {
return viteSsrExportsSquared;
},
});
viteSsrExports.squared(2) To avoid this, you need to benchmark without transforming, so you probably should try using pre-built version of |
Is there a workaround for this bug (without modifying code in // a.ts
export const fn = () => {};
export const a = () => {
fn();
} // b.ts
import { fn } from "./a";
export const b = () => {
fn();
} // x.bench.ts
import { bench, describe } from "vitest"
import { a } from "./a"
import { b } from "./b"
describe("x", () => {
bench("a", () => {
a()
})
bench("a2", () => {
a()
})
bench("b", () => {
b()
})
bench("b2", () => {
b()
})
})
|
@crimx Here's how we handle it in es-toolkit:
deps: {
optimizer: {
ssr: {
enabled: true,
include: ['es-toolkit', 'es-toolkit/compat'],
},
},
}
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';
const chunkToolkit = chunkToolkit_;
const chunkCompatToolkit = chunkCompatToolkit_;
const chunkLodash = chunkLodash_;
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('es-toolkit/compat/chunk', () => {
chunkCompatToolkit([1, 2, 3, 4, 5, 6], 3);
});
}); After all this effort, our results are not too far off from tinybench (although there are still some gaps)
after
tinybench
We hope our efforts have inspired you. |
Describe the bug
I try the following code and run
vitest bench
Here's what I got:
You can see that a direct import statement using import is significantly slower than reassigning values
However, when I try tinybench like this:
There is no difference between them:
Reproduction
Use the code above
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: