Skip to content

Commit

Permalink
docs: Compare performance with object api (#49)
Browse files Browse the repository at this point in the history
* docs: Add object api perf comparison

* Simplify benchmark file
  • Loading branch information
alexnault authored Apr 26, 2023
1 parent 5906eec commit 2fc99a1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -31,7 +31,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
node-version: 20
registry-url: https://registry.npmjs.org/
cache: npm
- run: npm ci
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Sources: [classix](https://deno.bundlejs.com?q=classix), [clsx](https://deno.bun

![Performance comparison chart](media/perf.png)

Sources: Ran [benchmark](benchmark/) on an AMD Ryzen 5 5600x.
Sources: Ran [benchmark](benchmark/) on an AMD Ryzen 5 5600x with Node 20.

## Highlights

Expand Down
71 changes: 63 additions & 8 deletions benchmark/index.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
import benchmark from "benchmark";
import { performance } from "node:perf_hooks";
import classnames from "classnames";
import { clsx } from "clsx";
import { cx } from "classix";

import { cx as cxLocal } from "../dist/esm/classix.mjs";

new benchmark.Suite()
.add("classnames", () => classnames("class1", true && "class2"))
.add("clsx", () => clsx("class1", true && "class2"))
.add("classix", () => cx("class1", true && "class2"))
.add("classix (local)", () => cxLocal("class1", true && "class2"))
.on("cycle", (event) => console.log(event.target.toString()))
.run();
const NB_RUN = 100_000_000;

function logResult(name, ms) {
const millionsOfOpsPerSecond = ((NB_RUN * 1000) / ms / 1_000_000).toFixed(1);
console.log(`${name}: ${millionsOfOpsPerSecond}M ops/s`);
}

// Repetition of code was used as to not affect performance
let start = 0;
let stop = 0;

start = performance.now();
for (let i = 0; i < NB_RUN; i++) {
classnames("class-1", { "class-2": true });
}
stop = performance.now();
logResult("classnames (object)", stop - start);

//

start = performance.now();
for (let i = 0; i < NB_RUN; i++) {
classnames("class-1", true && "class-2");
}
stop = performance.now();
logResult("classnames", stop - start);

//

start = performance.now();
for (let i = 0; i < NB_RUN; i++) {
clsx("class-1", { "class-2": true });
}
stop = performance.now();
logResult("clsx (object)", stop - start);

//

start = performance.now();
for (let i = 0; i < NB_RUN; i++) {
clsx("class-1", true && "class-2");
}
stop = performance.now();
logResult("clsx", stop - start);

//

start = performance.now();
for (let i = 0; i < NB_RUN; i++) {
cx("class-1", true && "class-2");
}
stop = performance.now();
logResult("classix", stop - start);

//

start = performance.now();
for (let i = 0; i < NB_RUN; i++) {
cxLocal("class-1", true && "class-2");
}
stop = performance.now();
logResult("classix (local)", stop - start);
Binary file modified media/perf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 0 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"@vitest/coverage-c8": "^0.30.1",
"benchmark": "^2.1.4",
"classix": "^2.1.8",
"classnames": "^2.3.1",
"clsx": "^1.2.1",
Expand Down

0 comments on commit 2fc99a1

Please sign in to comment.