Skip to content

Commit

Permalink
Further size optimization (#31)
Browse files Browse the repository at this point in the history
* convert named functions to arrow ones

* reorganize functions code to make them shorter

* embed createColors to the default export directly

* reorganize isColorSupported to make it shorter

* reorganize functions code again to preserve the speed

* Revert "embed createColors to the default export directly"

This reverts commit 22392c3.

* Revert "reorganize isColorSupported to make it shorter"

This reverts commit 59df46d.

* omit parens when possible
  • Loading branch information
borodean authored Oct 13, 2021
1 parent 1ca2bd0 commit 147fbdb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 47 deletions.
4 changes: 2 additions & 2 deletions benchmarks/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ suite
picocolors.yellow(`${++index}`)
)
})
.on("cycle", (event) => {
.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) => {
.on("error", event => {
process.stderr.write(picocolors.red(event.target.error.toString()) + "\n")
process.exit(1)
})
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ for (let i = 0; i < RUNS; i++) {
output
.trim()
.split("\n")
.forEach((line) => {
.forEach(line => {
let [name, result] = line.split(" ")
results[name] = (results[name] || 0) + parseFloat(result)
})
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ suite
.add("picocolors", () => {
out = picocolors.red("Add plugin to use time limit")
})
.on("cycle", (event) => {
.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) => {
.on("error", event => {
process.stderr.write(picocolors.red(event.target.error.toString()) + "\n")
process.exit(1)
})
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/size.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ let { get } = require("https")
let { bold, gray } = require("../picocolors.js")

async function getJSON(url) {
return new Promise((resolve) => {
get(url, (res) => {
return new Promise(resolve => {
get(url, res => {
let text = ""
res.on("data", (chunk) => {
res.on("data", chunk => {
text += chunk
})
res.on("end", () => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"printWidth": 100,
"useTabs": true,
"tabWidth": 2,
"semi": false
"semi": false,
"arrowParens": "avoid"
},
"clean-publish": {
"cleanDocs": true
Expand Down
74 changes: 36 additions & 38 deletions picocolors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,51 @@ let isColorSupported =
(tty.isatty(1) && process.env.TERM !== "dumb") ||
"CI" in process.env)

function formatter(open, close, replace = open) {
return (input) => {
let formatter =
(open, close, replace = open) =>
input => {
let string = "" + input
let index = string.indexOf(close, open.length)
return !~index
? open + string + close
: open + replaceClose(string, close, replace, index) + close
return ~index
? open + replaceClose(string, close, replace, index) + close
: open + string + close
}
}

function replaceClose(string, close, replace, index) {
let replaceClose = (string, close, replace, index) => {
let start = string.substring(0, index) + replace
let end = string.substring(index + close.length)
let nextIndex = end.indexOf(close)
return !~nextIndex ? start + end : start + replaceClose(end, close, replace, nextIndex)
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
}

function createColors(enabled = isColorSupported) {
return {
isColorSupported: enabled,
reset: enabled ? (s) => `\x1b[0m${s}\x1b[0m` : String,
bold: enabled ? formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m") : String,
dim: enabled ? formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m") : String,
italic: enabled ? formatter("\x1b[3m", "\x1b[23m") : String,
underline: enabled ? formatter("\x1b[4m", "\x1b[24m") : String,
inverse: enabled ? formatter("\x1b[7m", "\x1b[27m") : String,
hidden: enabled ? formatter("\x1b[8m", "\x1b[28m") : String,
strikethrough: enabled ? formatter("\x1b[9m", "\x1b[29m") : String,
black: enabled ? formatter("\x1b[30m", "\x1b[39m") : String,
red: enabled ? formatter("\x1b[31m", "\x1b[39m") : String,
green: enabled ? formatter("\x1b[32m", "\x1b[39m") : String,
yellow: enabled ? formatter("\x1b[33m", "\x1b[39m") : String,
blue: enabled ? formatter("\x1b[34m", "\x1b[39m") : String,
magenta: enabled ? formatter("\x1b[35m", "\x1b[39m") : String,
cyan: enabled ? formatter("\x1b[36m", "\x1b[39m") : String,
white: enabled ? formatter("\x1b[37m", "\x1b[39m") : String,
gray: enabled ? formatter("\x1b[90m", "\x1b[39m") : String,
bgBlack: enabled ? formatter("\x1b[40m", "\x1b[49m") : String,
bgRed: enabled ? formatter("\x1b[41m", "\x1b[49m") : String,
bgGreen: enabled ? formatter("\x1b[42m", "\x1b[49m") : String,
bgYellow: enabled ? formatter("\x1b[43m", "\x1b[49m") : String,
bgBlue: enabled ? formatter("\x1b[44m", "\x1b[49m") : String,
bgMagenta: enabled ? formatter("\x1b[45m", "\x1b[49m") : String,
bgCyan: enabled ? formatter("\x1b[46m", "\x1b[49m") : String,
bgWhite: enabled ? formatter("\x1b[47m", "\x1b[49m") : String,
}
}
let createColors = (enabled = isColorSupported) => ({
isColorSupported: enabled,
reset: enabled ? s => `\x1b[0m${s}\x1b[0m` : String,
bold: enabled ? formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m") : String,
dim: enabled ? formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m") : String,
italic: enabled ? formatter("\x1b[3m", "\x1b[23m") : String,
underline: enabled ? formatter("\x1b[4m", "\x1b[24m") : String,
inverse: enabled ? formatter("\x1b[7m", "\x1b[27m") : String,
hidden: enabled ? formatter("\x1b[8m", "\x1b[28m") : String,
strikethrough: enabled ? formatter("\x1b[9m", "\x1b[29m") : String,
black: enabled ? formatter("\x1b[30m", "\x1b[39m") : String,
red: enabled ? formatter("\x1b[31m", "\x1b[39m") : String,
green: enabled ? formatter("\x1b[32m", "\x1b[39m") : String,
yellow: enabled ? formatter("\x1b[33m", "\x1b[39m") : String,
blue: enabled ? formatter("\x1b[34m", "\x1b[39m") : String,
magenta: enabled ? formatter("\x1b[35m", "\x1b[39m") : String,
cyan: enabled ? formatter("\x1b[36m", "\x1b[39m") : String,
white: enabled ? formatter("\x1b[37m", "\x1b[39m") : String,
gray: enabled ? formatter("\x1b[90m", "\x1b[39m") : String,
bgBlack: enabled ? formatter("\x1b[40m", "\x1b[49m") : String,
bgRed: enabled ? formatter("\x1b[41m", "\x1b[49m") : String,
bgGreen: enabled ? formatter("\x1b[42m", "\x1b[49m") : String,
bgYellow: enabled ? formatter("\x1b[43m", "\x1b[49m") : String,
bgBlue: enabled ? formatter("\x1b[44m", "\x1b[49m") : String,
bgMagenta: enabled ? formatter("\x1b[45m", "\x1b[49m") : String,
bgCyan: enabled ? formatter("\x1b[46m", "\x1b[49m") : String,
bgWhite: enabled ? formatter("\x1b[47m", "\x1b[49m") : String,
})

module.exports = createColors()
module.exports.createColors = createColors

0 comments on commit 147fbdb

Please sign in to comment.