diff --git a/README.md b/README.md index 17a8a48..6d82c79 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ https://badgen.now.sh/ `npm run bench` on my iMac5K(Late 2014), 3.5G i5, with Node.js 10.5.0: ```bash -generate by short params x 717,411 ops/sec ±0.79% (89 runs sampled) -generate by long params x 692,485 ops/sec ±0.87% (93 runs sampled) -generate by full params x 613,054 ops/sec ±0.97% (92 runs sampled) +generate by short params x 806,080 ops/sec ±0.98% (88 runs sampled) +generate by long params x 735,925 ops/sec ±0.85% (90 runs sampled) +generate by full params x 770,180 ops/sec ±0.75% (90 runs sampled) ``` ## License diff --git a/bench/index.js b/bench/index.js index 5aba683..b630aa7 100644 --- a/bench/index.js +++ b/bench/index.js @@ -1,9 +1,9 @@ const { Suite } = require('benchmark') const badgen = require('..') -const shortParams = { subject: 'b', status: 'p' } +const shortParams = { subject: '1', status: '2' } const longParams = { subject: 'build-build-build', status: 'passing-passing-passing' } -const fullParams = { subject: 'license', status: 'Apache-2.0', color: 'blue' } +const fullParams = { subject: 'license', status: 'MIT', color: 'cyan' } new Suite() .add('generate by short params', () => badgen(shortParams)) diff --git a/lib/calc-text-width.js b/lib/calc-text-width.js index bba704c..4499526 100644 --- a/lib/calc-text-width.js +++ b/lib/calc-text-width.js @@ -5,7 +5,9 @@ const Tahoma12Widths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, const Verdana12Widths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4.21875,4.72265625,5.5078125,9.8203125,7.62890625,12.9140625,8.71875,3.22265625,5.44921875,5.44921875,7.62890625,9.8203125,4.365234375,5.44921875,4.365234375,5.44921875,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,7.62890625,5.44921875,5.44921875,9.8203125,9.8203125,9.8203125,6.544921875,12,8.203125,8.2265625,8.37890625,9.24609375,7.587890625,6.896484375,9.3046875,9.017578125,5.05078125,5.455078125,8.314453125,6.6796875,10.11328125,8.9765625,9.4453125,7.236328125,9.4453125,8.34375,8.203125,7.39453125,8.783203125,8.203125,11.865234375,8.220703125,7.3828125,8.220703125,5.44921875,5.44921875,5.44921875,9.8203125,7.62890625,7.62890625,7.20703125,7.4765625,6.251953125,7.4765625,7.1484375,4.21875,7.4765625,7.59375,3.29296875,4.130859375,7.1015625,3.29296875,11.671875,7.59375,7.283203125,7.4765625,7.4765625,5.12109375,6.251953125,4.728515625,7.59375,7.1015625,9.8203125,7.1015625,7.1015625,6.3046875,7.6171875,5.44921875,7.6171875,9.8203125] function calcWidth (charWidthTable) { - const widthTable = charWidthTable.map(w => w + 0.4) // Add letter-spacing + const SHIFT = 10 + const LETTER_SPACING = 0.4 * SHIFT + const widthTable = charWidthTable.map(w => Math.round(w * SHIFT + LETTER_SPACING)) return function (text) { if (typeof text !== 'string') { return 0 @@ -17,7 +19,7 @@ function calcWidth (charWidthTable) { charCode = text[i].charCodeAt() total += widthTable[charCode < 127 ? charCode : 64] // "@" for overflows } - return total + return total / SHIFT } } }