2x times faster than chalk and use 5x less space in node_modules

Overview

Nano Colors

A tiny and fast Node.js library for formatting terminal text with ANSI colors.

  • It is 2 times faster than chalk. Both loading and calls.
  • No dependencies. It takes 5 times less space in node_modules than chalk.
  • Actively maintained. Used in many big projects like PostCSS or Browserslist.
  • Auto-detects color support. You can also toggle color mode manually.
  • Tree-shakable. We use a dual ESM/CJS package.
  • Supports Node.js ≥ 6, Deno and universal Node.js/browser projects.
import { green, bold } from 'nanocolors'

console.log(
  green(`Task ${bold('1')} was finished`)
)

Nano Colors output

API was heavily inspired by @jorgebucaran’s colorette with few cool hacks copied from @lukeed’s kleur.

Sponsored by Evil Martians

Benchmarks

Function calling time:

$ ./test/benchmark.js
chalk         11,608,010 ops/sec
cli-color        752,419 ops/sec
ansi-colors    3,601,857 ops/sec
kleur         15,185,239 ops/sec
kleur/colors  21,113,231 ops/sec
colorette     19,712,884 ops/sec
nanocolors    21,176,376 ops/sec

Library loading time:

$ ./test/loading.js
chalk          3.465 ms
cli-color     21.849 ms
ansi-colors    1.101 ms
kleur          1.628 ms
kleur/colors   0.508 ms
colorette      2.610 ms
nanocolors     0.486 ms

The space in node_modules including sub-dependencies:

$ ./test/size.js
Data from packagephobia.com
chalk         101 kB
cli-color    1249 kB
ansi-colors    25 kB
kleur          21 kB
colorette      16 kB
nanocolors     16 kB

Test configuration: ThinkPad X1 Carbon Gen 9, Fedora 34, Node.js 16.8.

Replacing chalk

  1. Replace import and use named exports:

    - import chalk from 'chalk'
    + import { red, bold } from 'nanocolors'
  2. Unprefix calls:

    - chalk.red(text)
    + red(text)
  3. Replace chains to nested calls:

    - chalk.red.bold(text)
    + red(bold(text))

API

Individual Colors

Nano Colors exports functions:

Colors Background Colors Modifiers
black bgBlack dim
red bgRed bold
green bgGreen hidden
yellow bgYellow italic
blue bgBlue underline
magenta bgMagenta strikethrough
cyan bgCyan reset
white bgWhite
gray

Functions are not chainable. You need to wrap it inside each other:

import { black, bgYellow } from 'nanocolors'

console.log(bgYellow(black(' WARN ')))

Functions will use colors only if Nano Colors auto-detect that current environment supports colors.

You can get support level in isColorSupported:

import { isColorSupported } from 'nanocolors'

if (isColorSupported) {
  console.log('With colors')
}

Conditional Support

You can manually switch colors on/off and override color support auto-detection:

import { createColors } from 'nanocolors'

const { red } = createColors(options.enableColors)

On undefined argument, createColors will use value from color support auto-detection.

Deno

Nano Colors has build-in Deno support.

import { red } from 'https://deno.land/x/nanocolors/mod.ts'
Comments
  • Stolen library and fame

    Stolen library and fame

    You went way out of your way to look like you invented nanocolors, when it's actually someone else's work whose background you intentionally hid

    You should destroy this repository and apologize. All those users and that money belong to him, and you know it.

    All of your work is now suspect.

    I have removed your work from every repo I have control over, and I am encouraging everyone I know to do the same.

    You are an unrepentant plagiarist demanding recognition from your victim.

    opened by StoneCypher 22
  • nanocolors failing Jest tests

    nanocolors failing Jest tests

    Hey, I'm trying to use yours truly, nanocolors instead of chalk in a simple JS program and I keep getting this error when running npm test on the test.js file

    image

    App works perfectly fine when run, ( has import { red, blue } from 'nanocolors'; @ the top ) and the class tested is being exported.

    Any ideas? Just substituting nanocolors imports and syntax to chalkmakes Jest run the test fine.🙆‍♂️

    opened by dan-dm 13
  • Specify copyright holder properly.

    Specify copyright holder properly.

    1. In the author section of "LICENSE" file, only copyright holders should be listed. It is true that Bucaran is the author of the colorette repo, but it doesn't mean that Bucaran automatically become a copyright holder of all derived works.

    2. Since nanocolors is derived work of colorette which is licensed under MIT license, the colorette's LICENSE term should be included in nanocolors' source code. I know it's lengthy but this is the proper way to use MIT licensed source code. Look at this term:

      The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    By the way I'm grateful to see that Bucaran didn't DMCA takedown your repo this time.

    opened by simnalamburt 8
  • Added felt-pen & More accurate benchmarking

    Added felt-pen & More accurate benchmarking

    node .\complex-benchmark.js
    chalk          1,201,276 ops/sec
    cli-color        135,233 ops/sec
    ansi-colors      719,065 ops/sec
    kleur          1,404,775 ops/sec
    kleur/colors   1,471,413 ops/sec
    colorette      1,603,018 ops/sec
    felt-pen       1,489,272 ops/sec
    nanocolors     1,674,054 ops/sec
    
    chalk         101 kB
    cli-color    1249 kB
    ansi-colors    25 kB
    kleur          21 kB
    colorette      16 kB
    felt-pen        6 kB
    nanocolors     16 kB
    
    opened by nin-jin 6
  • kleur/colors

    kleur/colors

    Hey, welcome to the color space lol

    You should be benching against kleur/colors module since that's the direct comparison to yours and colorette. The root kleur module is the chalk API which is slower, slower to load, and not tree shakable by design.

    Thanks :)

    opened by lukeed 6
  • Add template API

    Add template API

    People like chalk’s template API a lot. We need to find a way to copy it in a form:

    import { colorize } from 'nanocolors'
    
    colorize`CPU: {red ${cpu.totalPercent}%}`
    
    opened by ai 4
  • Way to disable colors manually via js?

    Way to disable colors manually via js?

    I like to do this in my test

    import chai from 'chai';
    import { isColorSupported  } from 'nanocolors';
    import { executeCli } from './test-helpers.js';
    
    const { expect } = chai;
    
    describe('cli', () => {
      before(() => {
        // ignore colors in tests as most CIs won't support it
        isColorSupported = false;
      });
    

    which fails as you can't change variables you import?

    maybe isColorSupported should be an object?

    or is there another way to disable it while running the test locally that does not require a cli flag?

    opened by daKmoR 3
  • Publish an ES module

    Publish an ES module

    index.browser.js wouldn't actually work in a browser since its a commonjs module.

    what'd be really nice is to replace this with an ES module ("module": "ES2020" for example when running tsc).

    then we would be able to load the module directly in browsers, deno, node ESM, etc.

    though you may need to add an exports field in your package.json to make node detect it properly:

      "exports": {
        "import": "./index.browser.js",
        "require": "./index.js"
      },
    
    opened by 43081j 3
  • Added felt-pen to loading benchmark

    Added felt-pen to loading benchmark

    node .\loading.cjs
    chalk         13.964 ms
    cli-color     98.851 ms
    ansi-colors    3.832 ms
    kleur          7.182 ms
    kleur/colors   1.469 ms
    colorette      2.221 ms
    felt-pen       1.424 ms
    nanocolors     2.494 ms
    
    opened by nin-jin 1
  • Add automation instructions to README

    Add automation instructions to README

    It seems migration from chalk to nanocolors can be easily automated using jscodeshift. I made the simple transformer (I put in a gist here and added instructions of how apply it in the README

    opened by gavrix 1
  • Benchmark result needs updating

    Benchmark result needs updating

    Just released Colorette 2.0.11 with further perf improvements. This is what I'm seeing in the CI.

    • https://github.com/jorgebucaran/color-bench-test/runs/3710045880?check_suite_focus=true
    Screen Shot 2021-09-26 at 7 58 34

    If you run your benchmarks in the CI you'd avoid copy & pasting from your terminal all the time.

    Would be nice if you could update your README with these results (or remove outdated results).

    opened by jorgebucaran 1
Owner
Andrey Sitnik
The creator of Autoprefixer, @postcss, @browserslist, and @logux
Andrey Sitnik
Projeto individual, um site para cobertura de partidas de vôlei, times onde você pode saber onde, quando acontecerá as partidas, e dados sobre os times.

?? Volleyball Esports Coverage Um portal de vôlei para as pessoas se conectarem ou divulgarem suas partidas, conhecimentos e uma maneira de conhecerem

PedroJsn 4 Jun 6, 2022
👌A useful zero-dependencies, less than 434 Bytes (gzipped), pure JavaScript & CSS solution for drop an annoying pop-ups confirming the submission of form in your web apps.

Throw out pop-ups confirming the submission of form! A useful zero-dependencies, less than 434 Bytes (gzipped), pure JavaScript & CSS solution for dro

Vic Shóstak 35 Aug 24, 2022
Plug-and-play, faster-than-native promise/callback event emitter

kNow Blazing-fast callback/promise-based events with a tiny footprint What is this? With kNow (pronounced "now"—the k's silent), JavaScript event mana

Elijah Bodden 25 Sep 11, 2022
🎮 The only Front-End Performance Checklist that runs faster than the others

Front-End Performance Checklist ?? The only Front-End Performance Checklist that runs faster than the others. One simple rule: "Design and code with p

David Dias 15.5k Jan 1, 2023
An all new Titanfall VPK unpacker. Over 2x faster than the most popular alternative!

Harmony VPK Tool An electron-based app for unpacking Respawn VPK files. Super-fast and made with ♥ Why use Harmony VPK Tool over cra0's VPKTool? It's

Harmony 16 Dec 19, 2022
Pack all your node_modules and other files you want inside your project to a zip file.

?? Node Modules Packer Use Cases | Usage | Examples | Headless | Benchmarks | Reference This is a library to package all your node_modules and other f

Vinicius Lourenço 14 Dec 1, 2022
A cool tool that saves you time if you want to remove node_modules before running 'npm i'

rmnpm A cool tool that saves you time if you want to remove your node_modules folder before running the npm install command. How does it do it? By fir

null 4 Jul 16, 2022
Remove unnecessary files from node_modules (.md, .ts, ...)

What? node-prune is a small tool to prune unnecessary files from ./node_modules, such as markdown, typescript source files, and so on. Primarily built

TJ Holowaychuk 4.3k Jan 1, 2023
A web application for a company that provides commercial and scientific space travel services. The application will allow users to book rockets and join selected space missions.

Space Travelers A web application for a company that provides commercial and scientific space travel services. The application will allow users to boo

Hector Torres 2 Apr 6, 2022
This web application provides commercial and scientific space travel services. The application allows users to book rockets and join selected space missions.

space-hub About Project "Space Traveler's Hub" is A web application that provides commercial and scientific space travelling services, We are working

Nicholas Emmanuel 7 Nov 2, 2022
This a web application for a company that provides commercial and scientific space travel services. The application will allow users to book rockets, dragons and join selected space missions.

Space Travelers' Hub In this project, we have worked with the real live data from the SpaceX API. Our task was to build a web application for a compan

Apuabi Titilope 4 Oct 31, 2022
In this project we built a web application that consumes an SpaceX API. It provides commercial and scientific space travel services that allows users to book rockets and join selected space missions.

Space Travelers' Hub In this project we built a web application that consumes an SpaceX API. It provides commercial and scientific space travel servic

Diego Yon 7 Sep 30, 2022
This web application provides commercial and scientific space travel services. The application allows users to book rockets and join selected space missions.

Space Traveler's Hub This web application provides commercial and scientific space travel services. The application allows users to book rockets and j

Michael Mesfin 6 Oct 4, 2022
Space Travelers' Hub - a web application that facilitates booking rockets and join selected space missions

This is a web application that facilitates booking rockets and join selected space missions. It is built for a company that offers both commercial and scientific space travel services. The application also works with real live data from the SpaceX API.

Mong'are 6 Mar 29, 2022
"Space-Travelers-Hub" is a website that allows users to book rockets and join selected space missions by using data from the SpaceX API.

Space-Travelers-Hub This project was bootstrapped with Create React App. Description "Space-Travelers-Hub" is a website that allows users to book rock

Tresor Sawasawa 4 Mar 13, 2022
Welcome to Space Traveler's HUB, this web app allows the user to take a fictional tour across the space.

Welcome to Space Traveler's HUB, this web app allows the user to take a fictional tour across the space. The user can choose a rocket from our catalog interface, and reserve it. Also, the user can see recent special missions and join them. Finally, the user will be able to keep track of all your rockets and mission they are subscribed to. Build with React, Redux, React-router, and Railwindcss.

Mihreteab Misganaw 3 Jan 27, 2022
There can be more than Notion and Miro. Affine is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

AFFiNE.PRO The Next-Gen Knowledge Base to Replace Notion & Miro. Planning, Sorting and Creating all Together. Open-source, Privacy-First, and Free to

Toeverything 12.1k Jan 9, 2023