Files
shields/lib/make-badge.spec.js
Paul Melnikow cc9a6db853 Speed up font-width computation in most cases (#1390)
Ref: #1379

This takes a naive approach to font-width computation, the most compute-intensive part of rendering badges.

1. Add the widths of the individual characters.
    - These widths are measured on startup using PDFKit.
2. For each character pair, add a kerning adjustment
    - The difference between the width of each character pair, and the sum of the characters' separate widths.
    - These are computed for each character pair on startup using PDFKit.
3. For a string with characters outside the printable ASCII character set, fall back to PDFKit.

This branch averaged 0.041 ms in `makeBadge`, compared to 0.144 ms on master, a speedup of 73%. That was on a test of 10,000 consecutive requests (using the `benchmark-performance.sh` script, now checked in).

The speedup applies to badges containing exclusively printable ASCII characters. It wouldn't be as dramatic on non-ASCII text. Though, we could add some frequently used non-ASCII characters to the cached set.
2017-12-26 23:57:46 -05:00

42 lines
1.5 KiB
JavaScript

'use strict';
const assert = require('assert');
const { _badgeKeyWidthCache } = require('./make-badge');
const isSvg = require('is-svg');
const testHelpers = require('./make-badge-test-helpers');
const makeBadge = testHelpers.makeBadge();
describe('The badge generator', function () {
beforeEach(function () {
_badgeKeyWidthCache.clear();
});
it('should produce SVG', function () {
const svg = makeBadge({ text: ['cactus', 'grown'], format: 'svg' });
assert.ok(isSvg(svg));
assert(svg.includes('cactus'), 'cactus');
assert(svg.includes('grown'), 'grown');
});
it('should cache width of badge key', function () {
makeBadge({ text: ['cached', 'not-cached'], format: 'svg' });
assert.deepEqual([..._badgeKeyWidthCache.cache.keys()], ['cached']);
});
});
describe('"for-the-badge" template badge generation', function () {
// https://github.com/badges/shields/issues/1280
it('numbers should produce a string', function () {
const svg = makeBadge({ text: [1998, 1999], format: 'svg', template: 'for-the-badge' });
assert(svg.includes('1998'), `${svg} does not contain '1998'`);
assert(svg.includes('1999'), `${svg} does not contain '1999'`);
});
it('lowercase/mixedcase string should produce uppercase string', function () {
const svg = makeBadge({ text: ["Label", "1 string"], format: 'svg', template: 'for-the-badge' });
assert(svg.includes('LABEL'), `${svg} does not contain 'LABEL'`);
assert(svg.includes('1 STRING'), `${svg} does not contain '1 TEST'`);
});
});