Skip to content

Commit

Permalink
Add chalk v5 and yoctocolors to benchmarks (#88)
Browse files Browse the repository at this point in the history
* apply prettier conditionally

* add chalk v5 and yoctocolors to benchmarks

* node v6 build compat

* fine, save
  • Loading branch information
alexeyraspopov authored Oct 16, 2024
1 parent 5b01210 commit 6f0a463
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 108 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/benchmarks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ jobs:
run: node tests/environments.js
- name: Install esbuild
run: npm install esbuild
- name: Install missing libs
run: npm install chalk5@"npm:chalk@v5.x"
- name: Simple API calls
run: node benchmarks/simple.mjs
run: node benchmarks/simple.mjs --expose-gc
- name: Complex formatting expression
run: node benchmarks/complex.mjs
run: node benchmarks/complex.mjs --expose-gc
- name: Library module's init time
run: node benchmarks/loading.mjs
run: node benchmarks/loading.mjs --expose-gc
- name: Total loaded code size
run: node benchmarks/size.mjs
1 change: 0 additions & 1 deletion .prettierignore

This file was deleted.

32 changes: 31 additions & 1 deletion benchmarks/complex.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/* @prettier */
import { run, bench, summary } from "mitata"

import * as colorette from "colorette"
import kleur from "kleur"
import * as kleurColors from "kleur/colors"
import chalk from "chalk"
import chalk5 from "chalk5"
import ansi from "ansi-colors"
import cliColor from "cli-color"
import picocolors from "../picocolors.js"
import * as nanocolors from "nanocolors"
import * as yoctocolors from "yoctocolors"

summary(() => {
let index = 1e8

bench(
"chalk",
"chalk v4",
() =>
chalk.red(".") +
chalk.yellow(".") +
Expand All @@ -24,6 +27,33 @@ summary(() => {
)
)

bench(
"chalk v5",
() =>
chalk5.red(".") +
chalk5.yellow(".") +
chalk5.green(".") +
chalk5.bgRed(chalk5.black(" ERROR ")) +
chalk5.red(
" Add plugin " + chalk5.yellow("name") + " to use time limit with " + chalk5.yellow(++index)
)
)

bench(
"yoctocolors",
() =>
yoctocolors.red(".") +
yoctocolors.yellow(".") +
yoctocolors.green(".") +
yoctocolors.bgRed(yoctocolors.black(" ERROR ")) +
yoctocolors.red(
" Add plugin " +
yoctocolors.yellow("name") +
" to use time limit with " +
yoctocolors.yellow(++index)
)
)

bench(
"cli-color",
() =>
Expand Down
75 changes: 59 additions & 16 deletions benchmarks/loading.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @prettier */
import { run, bench, group } from "mitata"
import { buildSync } from "esbuild"
import { createRequire } from "module"
Expand All @@ -12,6 +13,8 @@ function build(contents) {
bundle: true,
write: false,
platform: "node",
format: "cjs",
target: "es2020",
stdin: { contents, loader: "js", resolveDir: root },
})
let code = result.outputFiles[0].text
Expand All @@ -23,36 +26,76 @@ function compile(code, context) {
}

group(() => {
let codeP = build(`let picocolors = require("../picocolors.js")`)
let contextP = createContext({ require: createRequire(filename), process: { env: {} } })
let codeP = build(
`let picocolors = require("../picocolors.js"); console.log(picocolors != null);`
)
let contextP = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
bench("picocolors", () => compile(codeP, contextP))

let codeAC = build(`let ansi = require("ansi-colors")`)
let contextAC = createContext({ require: createRequire(filename), process: { env: {} } })
let codeAC = build(`let ansi = require("ansi-colors"); console.log(ansi != null);`)
let contextAC = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
bench("ansi-colors", () => compile(codeAC, contextAC))

let codeK = build(`let kleur = require("kleur")`)
let contextK = createContext({ require: createRequire(filename), process: { env: {} } })
let codeK = build(`let kleur = require("kleur"); console.log(kleur != null);`)
let contextK = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
bench("kleur", () => compile(codeK, contextK))

let codeKC = build(`let kleurColors = require("kleur/colors")`)
let contextKC = createContext({ require: createRequire(filename), process: { env: {} } })
let codeKC = build(`let kleurColors = require("kleur/colors"); console.log(kleurColors != null);`)
let contextKC = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
bench("kleur/colors", () => compile(codeKC, contextKC))

let codeC = build(`let colorette = require("colorette")`)
let contextC = createContext({ require: createRequire(filename), process: { env: {} } })
let codeC = build(`let colorette = require("colorette"); console.log(colorette != null);`)
let contextC = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
bench("colorette", () => compile(codeC, contextC))

let codeN = build(`let nanocolors = require("nanocolors")`)
let contextN = createContext({ require: createRequire(filename), process: { env: {} } })
let codeN = build(`let nanocolors = require("nanocolors"); console.log(nanocolors != null);`)
let contextN = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
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 codeY = build(`import * as yoctocolors from "yoctocolors"; console.log(yoctocolors != null);`)
let contextY = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
bench("yoctocolors", () => compile(codeY, contextY))

let codeCh = build(`let chalk = require("chalk"); console.log(chalk != null);`)
let contextCh = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
bench("chalk v4", () => compile(codeCh, contextCh))

let codeCh5 = build(`import * as chalk5 from "chalk5"; console.log(chalk5 != null);`)
let contextCh5 = createContext({
require: createRequire(filename),
process: { env: { FORCE_COLOR: 1 } },
})
bench("chalk v5", () => compile(codeCh5, contextCh5))

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

Expand Down
13 changes: 12 additions & 1 deletion benchmarks/recursion.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
/* @prettier */
import { run, bench, summary } from "mitata"

import * as colorette from "colorette"
import kleur from "kleur"
import * as kleurColors from "kleur/colors"
import chalk from "chalk"
import chalk5 from "chalk5"
import ansi from "ansi-colors"
import cliColor from "cli-color"
import picocolors from "../picocolors.js"
import * as nanocolors from "nanocolors"
import * as yoctocolors from "yoctocolors"

let count = 1000
let input = "lorem ipsum dolor sit amet"

summary(() => {
bench("chalk", () => {
bench("chalk v4", () => {
return chalk.blue(chalk.red(input).repeat(count))
})

bench("chalk v5", () => {
return chalk5.blue(chalk5.red(input).repeat(count))
})

bench("cli-color", () => {
return cliColor.blue(cliColor.red(input).repeat(count))
})
Expand All @@ -41,6 +48,10 @@ summary(() => {
return nanocolors.blue(nanocolors.red(input).repeat(count))
})

bench("yoctocolors", () => {
return yoctocolors.blue(yoctocolors.red(input).repeat(count))
})

bench("picocolors", () => {
return picocolors.blue(picocolors.red(input).repeat(count))
})
Expand Down
13 changes: 12 additions & 1 deletion benchmarks/simple.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
/* @prettier */
import { run, bench, boxplot } from "mitata"

import * as colorette from "colorette"
import kleur from "kleur"
import * as kleurColors from "kleur/colors"
import chalk from "chalk"
import chalk5 from "chalk5"
import ansi from "ansi-colors"
import cliColor from "cli-color"
import picocolors from "../picocolors.js"
import * as nanocolors from "nanocolors"
import * as yoctocolors from "yoctocolors"

console.log(colorette.green("colorette"))
console.log(kleur.green("kleur"))
console.log(chalk.green("chalk"))
console.log(chalk5.green("chalk5"))
console.log(ansi.green("ansi"))
console.log(cliColor.green("cliColor"))
console.log(picocolors.green("picocolors"))
console.log(nanocolors.green("nanocolors"))
console.log(yoctocolors.green("yoctocolors"))

boxplot(() => {
bench("chalk", () => {
bench("chalk v4", () => {
return chalk.red("Add plugin to use time limit")
})
bench("chalk v5", () => {
return chalk5.red("Add plugin to use time limit")
})
bench("cli-color", () => {
return cliColor.red("Add plugin to use time limit")
})
Expand All @@ -39,6 +47,9 @@ boxplot(() => {
bench("nanocolors", () => {
return nanocolors.red("Add plugin to use time limit")
})
bench("yoctocolors", () => {
return yoctocolors.red("Add plugin to use time limit")
})
bench("picocolors", () => {
return picocolors.red("Add plugin to use time limit")
})
Expand Down
5 changes: 4 additions & 1 deletion benchmarks/size.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* @prettier */
import { buildSync } from "esbuild"
import { dirname } from "node:path"

console.table({
picocolors: build(`export { default as picocolors } from "../picocolors.js"`),
colorette: build(`export * as colorette from "colorette"`),
chalk: build(`export { default as chalk } from "chalk"`),
"chalk v4": build(`export { default as chalk } from "chalk"`),
"chalk v5": build(`export * as chalk from "chalk5"`),
kleur: build(`export { default as kleur } from "kleur"`),
"kleur/colors": build(`export * as kleurColors from "kleur/colors"`),
"ansi-colors": build(`export { default as ansi } from "ansi-colors"`),
"cli-color": build(`export { default as cliColor } from "cli-color"`),
nanocolors: build(`export * as nanocolors from "nanocolors"`),
yoctocolors: build(`export * as yoctocolors from "yoctocolors"`),
})

function build(contents) {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@
"kleur": "^4.1.4",
"mitata": "^1.0.10",
"nanocolors": "^0.2.12",
"prettier": "^2.4.1"
"prettier": "^3.3.3",
"yoctocolors": "^2.1.1"
},
"prettier": {
"printWidth": 100,
"useTabs": true,
"tabWidth": 2,
"semi": false,
"arrowParens": "avoid"
"arrowParens": "avoid",
"requirePragma": true
},
"clean-publish": {
"cleanDocs": true
Expand Down
Loading

0 comments on commit 6f0a463

Please sign in to comment.