Skip to content

Commit

Permalink
Merge pull request #26 from alexeyraspopov/feature/ForceCJS
Browse files Browse the repository at this point in the history
The world is not ready for esm yet
  • Loading branch information
alexeyraspopov authored Oct 4, 2021
2 parents 2e4e023 + 6d92e1a commit 2bbb880
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 502 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig is awesome: https://EditorConfig.org

root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,d.ts}]
charset = utf-8
indent_style = tab
indent_size = 2
trim_trailing_whitespace = true

[package.json,*.yaml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: node tests/test.js
- run: node tests/test.cjs
legacy:
name: Node v${{ matrix.node-version }} (Legacy)
runs-on: ubuntu-latest
Expand All @@ -39,4 +38,4 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: node tests/test.cjs
- run: node tests/test.js
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
A tinier and faster alternative to [nanocolors](https://github.com/ai/nanocolors). Andrey, are you even trying?

```javascript
import { green, italic } from "picocolors";
import pc from "picocolors";

console.log(green(`How are ${italic(`you`)} doing?`));
console.log(pc.green(`How are ${pc.italic(`you`)} doing?`));
```

- Up to [2x faster and 2x smaller](#benchmarks) than alternatives
- 3x faster and 10x smaller than `chalk`
- [TypeScript](https://www.typescriptlang.org/) & [Flowtype](https://flow.org/) support
- [TypeScript](https://www.typescriptlang.org/) support
- [`NO_COLOR`](https://no-color.org/) friendly
- Node.js v6+ & browsers support
- The same API, but faster, much faster
- No `String.prototype` modifications (anyone still doing it?)
- No dependencies and the smallest `node_modules` footprint
- Tree-shakeable (in case a Node.js package needs it?)

## Prior Art

Expand Down Expand Up @@ -70,12 +69,3 @@ colorette × 657,896 ops/sec
nanocolors × 660,817 ops/sec
ansi-colors × 290,986 ops/sec
```

## Replacing nanocolors

Just replace imports

```diff
-import { green, italic } from 'nanocolors';
+import { green, italic } from 'picocolors';
```
190 changes: 95 additions & 95 deletions benchmarks/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,107 +5,107 @@
// 2. Run tests 5 times.
// 3. Took the best result for each candidate.

import benchmark from "benchmark";
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 * as 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");

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

const suite = new benchmark.Suite();
let suite = new benchmark.Suite();
let out;

let index = 1e8;

suite
.add("chalk", () => {
out =
chalk.bgRed.black(" ERROR ") +
chalk.red(
" Add plugin " + chalk.yellow("name") + " to use time limit with " + chalk.yellow(++index)
);
})
.add("cli-color", () => {
out =
cliColor.bgRed.black(" ERROR ") +
cliColor.red(
" Add plugin " +
cliColor.yellow("name") +
" to use time limit with " +
cliColor.yellow(++index)
);
})
.add("ansi-colors", () => {
out =
ansi.bgRed.black(" ERROR ") +
ansi.red(
" Add plugin " + ansi.yellow("name") + " to use time limit with " + ansi.yellow(++index)
);
})
.add("kleur", () => {
out =
kleur.bgRed().black(" ERROR ") +
kleur.red(
" Add plugin " + kleur.yellow("name") + " to use time limit with " + kleur.yellow(++index)
);
})
.add("kleur/colors", () => {
out =
kleurColors.bgRed(kleurColors.black(" ERROR ")) +
kleurColors.red(
" Add plugin " +
kleurColors.yellow("name") +
" to use time limit with " +
kleurColors.yellow(++index)
);
})
.add("colorette", () => {
out =
colorette.bgRed(colorette.black(" ERROR ")) +
colorette.red(
" Add plugin " +
colorette.yellow("name") +
" to use time limit with " +
colorette.yellow(++index)
);
})
.add("nanocolors", () => {
out =
nanocolors.bgRed(nanocolors.black(" ERROR ")) +
nanocolors.red(
" Add plugin " +
nanocolors.yellow("name") +
" to use time limit with " +
nanocolors.yellow(++index)
);
})
.add("picocolors", () => {
out =
picocolors.bgRed(picocolors.black(" ERROR ")) +
picocolors.red(
" Add plugin " +
picocolors.yellow("name") +
" to use time limit with " +
picocolors.yellow(`${++index}`)
);
})
.on("cycle", (event) => {
const prefix = event.target.name === "picocolors" ? "+ " : " ";
const name = event.target.name.padEnd("kleur/colors ".length);
const 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();
.add("chalk", () => {
out =
chalk.bgRed.black(" ERROR ") +
chalk.red(
" Add plugin " + chalk.yellow("name") + " to use time limit with " + chalk.yellow(++index)
);
})
.add("cli-color", () => {
out =
cliColor.bgRed.black(" ERROR ") +
cliColor.red(
" Add plugin " +
cliColor.yellow("name") +
" to use time limit with " +
cliColor.yellow(++index)
);
})
.add("ansi-colors", () => {
out =
ansi.bgRed.black(" ERROR ") +
ansi.red(
" Add plugin " + ansi.yellow("name") + " to use time limit with " + ansi.yellow(++index)
);
})
.add("kleur", () => {
out =
kleur.bgRed().black(" ERROR ") +
kleur.red(
" Add plugin " + kleur.yellow("name") + " to use time limit with " + kleur.yellow(++index)
);
})
.add("kleur/colors", () => {
out =
kleurColors.bgRed(kleurColors.black(" ERROR ")) +
kleurColors.red(
" Add plugin " +
kleurColors.yellow("name") +
" to use time limit with " +
kleurColors.yellow(++index)
);
})
.add("colorette", () => {
out =
colorette.bgRed(colorette.black(" ERROR ")) +
colorette.red(
" Add plugin " +
colorette.yellow("name") +
" to use time limit with " +
colorette.yellow(++index)
);
})
.add("nanocolors", () => {
out =
nanocolors.bgRed(nanocolors.black(" ERROR ")) +
nanocolors.red(
" Add plugin " +
nanocolors.yellow("name") +
" to use time limit with " +
nanocolors.yellow(++index)
);
})
.add("picocolors", () => {
out =
picocolors.bgRed(picocolors.black(" ERROR ")) +
picocolors.red(
" Add plugin " +
picocolors.yellow("name") +
" 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();
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const { performance } = require("perf_hooks");

let before;
function showTime(name) {
let after = performance.now();
process.stdout.write(name + " " + (after - before) + "\n");
let after = performance.now();
process.stdout.write(name + " " + (after - before) + "\n");
}

before = performance.now();
Expand Down Expand Up @@ -35,5 +35,5 @@ let nanocolors = require("nanocolors");
showTime("nanocolors");

before = performance.now();
let picocolors = require("../picocolors.cjs");
let picocolors = require("../picocolors.js");
showTime("picocolors");
38 changes: 19 additions & 19 deletions benchmarks/loading.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#!/usr/bin/env node

import { execSync } from "child_process";
let { execSync } = require("child_process");

const RUNS = 50;
let RUNS = 50;

const results = {};
let results = {};

for (let i = 0; i < RUNS; i++) {
const output = execSync("node ./benchmarks/loading-runner.cjs").toString();
output
.trim()
.split("\n")
.forEach((line) => {
let [name, result] = line.split(" ");
results[name] = (results[name] || 0) + parseFloat(result);
});
let output = execSync("node ./benchmarks/loading-runner.js").toString();
output
.trim()
.split("\n")
.forEach((line) => {
let [name, result] = line.split(" ");
results[name] = (results[name] || 0) + parseFloat(result);
});
}

for (const name in results) {
const prefix = name === "picocolors" ? "+ " : " ";
const title = name.padEnd("kleur/colors ".length);
const time = (Math.round((1000 * results[name]) / RUNS) / 1000)
.toString()
.replace(/\.\d$/, "$&00")
.replace(/\.\d\d$/, "$&0");
process.stdout.write(prefix + title + "\x1B[1m" + time.padStart(6) + "\x1B[22m ms\n");
for (let name in results) {
let prefix = name === "picocolors" ? "+ " : " ";
let title = name.padEnd("kleur/colors ".length);
let time = (Math.round((1000 * results[name]) / RUNS) / 1000)
.toString()
.replace(/\.\d$/, "$&00")
.replace(/\.\d\d$/, "$&0");
process.stdout.write(prefix + title + "\x1B[1m" + time.padStart(6) + "\x1B[22m ms\n");
}
Loading

0 comments on commit 2bbb880

Please sign in to comment.