-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add examples and run them in the ci
- Loading branch information
1 parent
c5b439f
commit 7bf2d29
Showing
36 changed files
with
930 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
auto-install-peers=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { withCodSpeed } = require("@codspeed/benchmark.js-plugin"); | ||
const Benchmark = require("benchmark"); | ||
|
||
const suite = withCodSpeed(new Benchmark.Suite()); | ||
|
||
suite | ||
.add("RegExp#test", function () { | ||
/o/.test("Hello World!"); | ||
}) | ||
.add("String#indexOf", function () { | ||
"Hello World!".indexOf("o") > -1; | ||
}) | ||
// add listeners | ||
.on("cycle", function (event) { | ||
console.log(String(event.target)); | ||
}) | ||
// run async | ||
.run({ async: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "with-javascript-cjs", | ||
"private": true, | ||
"scripts": { | ||
"bench-benchmark-js": "node benchmark-js.js", | ||
"bench-tinybench": "node tinybench.js" | ||
}, | ||
"devDependencies": { | ||
"@codspeed/benchmark.js-plugin": "workspace:^1.1.0", | ||
"@codspeed/tinybench-plugin": "workspace:^1.1.0", | ||
"benchmark": "^2.1.4", | ||
"tinybench": "^2.5.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { withCodSpeed } = require("@codspeed/tinybench-plugin"); | ||
const { Bench } = require("tinybench"); | ||
|
||
const bench = withCodSpeed(new Bench({ time: 100 })); | ||
|
||
bench | ||
.add("switch 1", () => { | ||
let a = 1; | ||
let b = 2; | ||
const c = a; | ||
a = b; | ||
b = c; | ||
}) | ||
.add("switch 2", () => { | ||
let a = 1; | ||
let b = 10; | ||
a = b + a; | ||
b = a - b; | ||
a = b - a; | ||
}); | ||
|
||
bench.run().then(() => { | ||
console.table( | ||
bench.tasks.map(({ name, result }) => ({ | ||
"Task Name": name, | ||
"Average Time (ps)": result?.mean ? result.mean * 1000 : "N/A", | ||
"Variance (ps)": result?.variance ? result.variance * 1000 : "N/A", | ||
})) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { withCodSpeed } from "@codspeed/benchmark.js-plugin"; | ||
import Benchmark from "benchmark"; | ||
|
||
const suite = withCodSpeed(new Benchmark.Suite()); | ||
|
||
suite | ||
.add("RegExp#test", function () { | ||
/o/.test("Hello World!"); | ||
}) | ||
.add("String#indexOf", function () { | ||
"Hello World!".indexOf("o") > -1; | ||
}) | ||
// add listeners | ||
.on("cycle", function (event) { | ||
console.log(String(event.target)); | ||
}) | ||
// run async | ||
.run({ async: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "with-javascript-esm", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"bench-benchmark-js": "node benchmark-js.js", | ||
"bench-tinybench": "node tinybench.js" | ||
}, | ||
"devDependencies": { | ||
"@codspeed/benchmark.js-plugin": "workspace:^1.1.0", | ||
"@codspeed/tinybench-plugin": "workspace:^1.1.0", | ||
"benchmark": "^2.1.4", | ||
"tinybench": "^2.5.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { withCodSpeed } from "@codspeed/tinybench-plugin"; | ||
import { Bench } from "tinybench"; | ||
|
||
const bench = withCodSpeed(new Bench({ time: 100 })); | ||
|
||
bench | ||
.add("switch 1", () => { | ||
let a = 1; | ||
let b = 2; | ||
const c = a; | ||
a = b; | ||
b = c; | ||
}) | ||
.add("switch 2", () => { | ||
let a = 1; | ||
let b = 10; | ||
a = b + a; | ||
b = a - b; | ||
a = b - a; | ||
}); | ||
|
||
bench.run().then(() => { | ||
console.table( | ||
bench.tasks.map(({ name, result }) => ({ | ||
"Task Name": name, | ||
"Average Time (ps)": result?.mean ? result.mean * 1000 : "N/A", | ||
"Variance (ps)": result?.variance ? result.variance * 1000 : "N/A", | ||
})) | ||
); | ||
}); |
35 changes: 35 additions & 0 deletions
35
examples/with-typescript-cjs/bench/benchmark.js/fibo.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { WithCodSpeedSuite } from "@codspeed/benchmark.js-plugin"; | ||
import { | ||
iterativeFibonacci, | ||
recursiveCachedFibonacci, | ||
recursiveFibonacci, | ||
} from "../../src/fibonacci"; | ||
|
||
export function registerFiboBenchmarks(suite: WithCodSpeedSuite) { | ||
suite | ||
.add("test_recursive_fibo_10", () => { | ||
recursiveFibonacci(10); | ||
}) | ||
.add("test_recursive_fibo_20", () => { | ||
recursiveFibonacci(20); | ||
}); | ||
|
||
suite | ||
.add("test_recursive_cached_fibo_10", () => { | ||
recursiveCachedFibonacci(10); | ||
}) | ||
.add("test_recursive_cached_fibo_20", () => { | ||
recursiveCachedFibonacci(20); | ||
}) | ||
.add("test_recursive_cached_fibo_30", () => { | ||
recursiveCachedFibonacci(30); | ||
}); | ||
|
||
suite | ||
.add("test_iterative_fibo_10", () => { | ||
iterativeFibonacci(10); | ||
}) | ||
.add("test_iterative_fibo_100", () => { | ||
iterativeFibonacci(100); | ||
}); | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/with-typescript-cjs/bench/benchmark.js/foobarbaz.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { WithCodSpeedSuite } from "@codspeed/benchmark.js-plugin"; | ||
import { aBaz, baz } from "../../src/foobarbaz"; | ||
|
||
export function registerFoobarbazBenchmarks(suite: WithCodSpeedSuite) { | ||
suite | ||
.add("test sync baz 10", () => { | ||
baz(10); | ||
}) | ||
.add("test sync baz 100", () => { | ||
baz(100); | ||
}); | ||
|
||
suite | ||
.add("test async baz 10", async () => { | ||
await aBaz(10); | ||
}) | ||
.add("test async baz 100", async () => { | ||
await aBaz(100); | ||
}); | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/with-typescript-cjs/bench/benchmark.js/index.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { withCodSpeed } from "@codspeed/benchmark.js-plugin"; | ||
import Benchmark from "benchmark"; | ||
import { registerFiboBenchmarks } from "./fibo.bench"; | ||
import { registerFoobarbazBenchmarks } from "./foobarbaz.bench"; | ||
|
||
export const suite = withCodSpeed(new Benchmark.Suite()); | ||
|
||
(async () => { | ||
registerFiboBenchmarks(suite); | ||
registerFoobarbazBenchmarks(suite); | ||
|
||
suite.on("cycle", function (event: Benchmark.Event) { | ||
console.log(String(event.target)); | ||
}); | ||
|
||
await suite.run({ async: true }); | ||
})(); |
35 changes: 35 additions & 0 deletions
35
examples/with-typescript-cjs/bench/tinybench/fibo.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Bench } from "tinybench"; | ||
import { | ||
iterativeFibonacci, | ||
recursiveCachedFibonacci, | ||
recursiveFibonacci, | ||
} from "../../src/fibonacci"; | ||
|
||
export function registerFiboBenchmarks(bench: Bench) { | ||
bench | ||
.add("test_recursive_fibo_10", () => { | ||
recursiveFibonacci(10); | ||
}) | ||
.add("test_recursive_fibo_20", () => { | ||
recursiveFibonacci(20); | ||
}); | ||
|
||
bench | ||
.add("test_recursive_cached_fibo_10", () => { | ||
recursiveCachedFibonacci(10); | ||
}) | ||
.add("test_recursive_cached_fibo_20", () => { | ||
recursiveCachedFibonacci(20); | ||
}) | ||
.add("test_recursive_cached_fibo_30", () => { | ||
recursiveCachedFibonacci(30); | ||
}); | ||
|
||
bench | ||
.add("test_iterative_fibo_10", () => { | ||
iterativeFibonacci(10); | ||
}) | ||
.add("test_iterative_fibo_100", () => { | ||
iterativeFibonacci(100); | ||
}); | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/with-typescript-cjs/bench/tinybench/foobarbaz.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Bench } from "tinybench"; | ||
import { aBaz, baz } from "../../src/foobarbaz"; | ||
|
||
export function registerFoobarbazBenchmarks(bench: Bench) { | ||
bench | ||
.add("test sync baz 10", () => { | ||
baz(10); | ||
}) | ||
.add("test sync baz 100", () => { | ||
baz(100); | ||
}); | ||
|
||
bench | ||
.add("test async baz 10", async () => { | ||
await aBaz(10); | ||
}) | ||
.add("test async baz 100", async () => { | ||
await aBaz(100); | ||
}); | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/with-typescript-cjs/bench/tinybench/index.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { withCodSpeed } from "@codspeed/tinybench-plugin"; | ||
import { Bench } from "tinybench"; | ||
import { registerFiboBenchmarks } from "./fibo.bench"; | ||
import { registerFoobarbazBenchmarks } from "./foobarbaz.bench"; | ||
|
||
export const bench = withCodSpeed(new Bench()); | ||
|
||
(async () => { | ||
registerFiboBenchmarks(bench); | ||
registerFoobarbazBenchmarks(bench); | ||
|
||
await bench.run(); | ||
console.table( | ||
bench.tasks.map(({ name, result }) => ({ | ||
"Task Name": name, | ||
"Average Time (ps)": result?.mean ? result.mean * 1000 : "N/A", | ||
"Variance (ps)": result?.variance ? result.variance * 1000 : "N/A", | ||
})) | ||
); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "with-typescript-cjs", | ||
"private": true, | ||
"scripts": { | ||
"bench-benchmark-js": "node -r esbuild-register bench/benchmark.js/index.bench.ts", | ||
"bench-tinybench": "node -r esbuild-register bench/tinybench/index.bench.ts" | ||
}, | ||
"devDependencies": { | ||
"@codspeed/benchmark.js-plugin": "workspace:^1.1.0", | ||
"@codspeed/tinybench-plugin": "workspace:^1.1.0", | ||
"@types/benchmark": "^2.1.2", | ||
"benchmark": "^2.1.4", | ||
"tinybench": "^2.5.0", | ||
"typescript": "^5.1.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export function recursiveFibonacci(n: number): number { | ||
if (n < 2) { | ||
return n; | ||
} | ||
return recursiveFibonacci(n - 1) + recursiveFibonacci(n - 2); | ||
} | ||
|
||
export function recursiveCachedFibonacci(n: number) { | ||
const cache: Record<number, number> = { 0: 0, 1: 1 }; | ||
const fiboInner = (n: number) => { | ||
if (n in cache) { | ||
return cache[n]; | ||
} | ||
cache[n] = | ||
recursiveCachedFibonacci(n - 1) + recursiveCachedFibonacci(n - 2); | ||
return cache[n]; | ||
}; | ||
return fiboInner(n); | ||
} | ||
|
||
export function iterativeFibonacci(n: number) { | ||
let a = 0; | ||
let b = 1; | ||
let c = 0; | ||
for (let i = 0; i < n; i++) { | ||
c = a + b; | ||
a = b; | ||
b = c; | ||
} | ||
return a; | ||
} |
Oops, something went wrong.