Skip to content

Commit

Permalink
Use mitata, esbuild for benchmarking speed and size (#86)
Browse files Browse the repository at this point in the history
* use mitata, esbuild for benchmarking speed and size

* move esbuild to bench job only

* update nodejs version for bench job
  • Loading branch information
alexeyraspopov authored Oct 10, 2024
1 parent 6a1eb72 commit 4c5e981
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 370 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/benchmarks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18
node-version: 22
- run: npm install
- name: Ensure color support detection
run: node tests/environments.js
- name: Install esbuild
run: npm install esbuild
- name: Simple API calls
run: node benchmarks/simple.js
run: node benchmarks/simple.mjs
- name: Complex formatting expression
run: node benchmarks/complex.js
run: node benchmarks/complex.mjs
- name: Library module's init time
run: node benchmarks/loading.js
- name: NPM package size
run: node benchmarks/size.js
run: node benchmarks/loading.mjs
- name: Total loaded code size
run: node benchmarks/size.mjs
114 changes: 53 additions & 61 deletions benchmarks/complex.js → benchmarks/complex.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
#!/usr/bin/env node
import { run, bench, summary } from "mitata"

// Benchmark results are unstable. To have more stable results:
// 1. Restart OS. Do not run any applications. Put power cable to laptop.
// 2. Run tests 5 times.
// 3. Took the best result for each candidate.
import * as colorette from "colorette"
import kleur from "kleur"
import * as kleurColors from "kleur/colors"
import chalk from "chalk"
import ansi from "ansi-colors"
import cliColor from "cli-color"
import picocolors from "../picocolors.js"
import * as nanocolors from "nanocolors"

let benchmark = require("benchmark")
let colorette = require("colorette")
let kleur = require("kleur")
let kleurColors = require("kleur/colors")
let chalk = require("chalk")
let ansi = require("ansi-colors")
let cliColor = require("cli-color")
let picocolors = require("../picocolors.js")
let nanocolors = require("nanocolors")
summary(() => {
let index = 1e8

function formatNumber(number) {
return String(number)
.replace(/\d{3}$/, ",$&")
.replace(/^(\d|\d\d)(\d{3},)/, "$1,$2")
}

let suite = new benchmark.Suite()
let out

let index = 1e8

suite
.add("chalk", () => {
out =
bench(
"chalk",
() =>
chalk.red(".") +
chalk.yellow(".") +
chalk.green(".") +
chalk.bgRed(chalk.black(" ERROR ")) +
chalk.red(
" Add plugin " + chalk.yellow("name") + " to use time limit with " + chalk.yellow(++index)
)
})
.add("cli-color", () => {
out =
)

bench(
"cli-color",
() =>
cliColor.red(".") +
cliColor.yellow(".") +
cliColor.green(".") +
Expand All @@ -49,29 +37,35 @@ suite
" to use time limit with " +
cliColor.yellow(++index)
)
})
.add("ansi-colors", () => {
out =
)

bench(
"ansi-colors",
() =>
ansi.red(".") +
ansi.yellow(".") +
ansi.green(".") +
ansi.bgRed(ansi.black(" ERROR ")) +
ansi.red(
" Add plugin " + ansi.yellow("name") + " to use time limit with " + ansi.yellow(++index)
)
})
.add("kleur", () => {
out =
)

bench(
"kleur",
() =>
kleur.red(".") +
kleur.yellow(".") +
kleur.green(".") +
kleur.bgRed(kleur.black(" ERROR ")) +
kleur.red(
" Add plugin " + kleur.yellow("name") + " to use time limit with " + kleur.yellow(++index)
)
})
.add("kleur/colors", () => {
out =
)

bench(
"kleur/colors",
() =>
kleurColors.red(".") +
kleurColors.yellow(".") +
kleurColors.green(".") +
Expand All @@ -82,9 +76,11 @@ suite
" to use time limit with " +
kleurColors.yellow(++index)
)
})
.add("colorette", () => {
out =
)

bench(
"colorette",
() =>
colorette.red(".") +
colorette.yellow(".") +
colorette.green(".") +
Expand All @@ -95,9 +91,11 @@ suite
" to use time limit with " +
colorette.yellow(++index)
)
})
.add("nanocolors", () => {
out =
)

bench(
"nanocolors",
() =>
nanocolors.red(".") +
nanocolors.yellow(".") +
nanocolors.green(".") +
Expand All @@ -108,9 +106,11 @@ suite
" to use time limit with " +
nanocolors.yellow(++index)
)
})
.add("picocolors", () => {
out =
)

bench(
"picocolors",
() =>
picocolors.red(".") +
picocolors.yellow(".") +
picocolors.green(".") +
Expand All @@ -121,15 +121,7 @@ suite
" to use time limit with " +
picocolors.yellow(`${++index}`)
)
})
.on("cycle", event => {
let prefix = event.target.name === "picocolors" ? "+ " : " "
let name = event.target.name.padEnd("kleur/colors ".length)
let hz = formatNumber(event.target.hz.toFixed(0)).padStart(10)
process.stdout.write(`${prefix}${name}${picocolors.bold(hz)} ops/sec\n`)
})
.on("error", event => {
process.stderr.write(picocolors.red(event.target.error.toString()) + "\n")
process.exit(1)
})
.run()
)
})

await run()
39 changes: 0 additions & 39 deletions benchmarks/loading-runner.js

This file was deleted.

28 changes: 0 additions & 28 deletions benchmarks/loading.js

This file was deleted.

59 changes: 59 additions & 0 deletions benchmarks/loading.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { run, bench, group } from "mitata"
import { buildSync } from "esbuild"
import { createRequire } from "module"
import { dirname } from "path"
import { createContext, compileFunction } from "vm"

let filename = new URL(import.meta.url).pathname

function build(contents) {
let root = dirname(filename)
let result = buildSync({
bundle: true,
write: false,
platform: "node",
stdin: { contents, loader: "js", resolveDir: root },
})
let code = result.outputFiles[0].text
return code
}

function compile(code, context) {
return compileFunction(code, [], { parsingContext: context })
}

group(() => {
let codeP = build(`let picocolors = require("../picocolors.js")`)
let contextP = createContext({ require: createRequire(filename), process: { env: {} } })
bench("picocolors", () => compile(codeP, contextP))

let codeAC = build(`let ansi = require("ansi-colors")`)
let contextAC = createContext({ require: createRequire(filename), process: { env: {} } })
bench("ansi-colors", () => compile(codeAC, contextAC))

let codeK = build(`let kleur = require("kleur")`)
let contextK = createContext({ require: createRequire(filename), process: { env: {} } })
bench("kleur", () => compile(codeK, contextK))

let codeKC = build(`let kleurColors = require("kleur/colors")`)
let contextKC = createContext({ require: createRequire(filename), process: { env: {} } })
bench("kleur/colors", () => compile(codeKC, contextKC))

let codeC = build(`let colorette = require("colorette")`)
let contextC = createContext({ require: createRequire(filename), process: { env: {} } })
bench("colorette", () => compile(codeC, contextC))

let codeN = build(`let nanocolors = require("nanocolors")`)
let contextN = createContext({ require: createRequire(filename), process: { env: {} } })
bench("nanocolors", () => compile(codeN, contextN))

let codeCh = build(`let chalk = require("chalk")`)
let contextCh = createContext({ require: createRequire(filename), process: { env: {} } })
bench("chalk", () => compile(codeCh, contextCh))

let codeCC = build(`let cliColor = require("cli-color")`)
let contextCC = createContext({ require: createRequire(filename), process: { env: {} } })
bench("cli-color", () => compile(codeCC, contextCC))
})

await run()
68 changes: 0 additions & 68 deletions benchmarks/recursion.js

This file was deleted.

Loading

0 comments on commit 4c5e981

Please # to comment.