|
| 1 | +import { bench } from '@paulmillr/jsbt/bench.js'; |
| 2 | +import { deepStrictEqual } from 'node:assert'; |
| 3 | +import decodeQR from '../src/decode.ts'; |
| 4 | +import { Bitmap, encodeQR } from '../src/index.ts'; |
| 5 | + |
| 6 | +const section = (name) => console.log(`\n# ${name}`); |
| 7 | + |
| 8 | +function imageFromText(text, opts = {}) { |
| 9 | + const { scale = 4, ...qrOpts } = opts; |
| 10 | + const raw = encodeQR(text, 'raw', { border: 4, ecc: 'medium', ...qrOpts }); |
| 11 | + return new Bitmap(raw.length, raw).scale(scale).toImage(); |
| 12 | +} |
| 13 | + |
| 14 | +const smallText = 'HELLO WORLD'; |
| 15 | +const largeText = 'H'.repeat(768); |
| 16 | +const smallImage = imageFromText(smallText, { version: 1, scale: 4 }); |
| 17 | +const largeImage = imageFromText(largeText, { scale: 2 }); |
| 18 | + |
| 19 | +async function main() { |
| 20 | + section('encode'); |
| 21 | + await bench('ascii small', () => encodeQR(smallText, 'ascii', { ecc: 'medium' })); |
| 22 | + await bench('gif small', () => encodeQR(smallText, 'gif', { ecc: 'medium' })); |
| 23 | + await bench('svg small', () => encodeQR(smallText, 'svg', { ecc: 'medium' })); |
| 24 | + |
| 25 | + section('encode large'); |
| 26 | + await bench('raw large', () => encodeQR(largeText, 'raw', { ecc: 'medium' })); |
| 27 | + await bench('ascii large', () => encodeQR(largeText, 'ascii', { ecc: 'medium' })); |
| 28 | + |
| 29 | + section('decode generated'); |
| 30 | + await bench('v1 scale4', () => deepStrictEqual(decodeQR(smallImage), smallText)); |
| 31 | + await bench('large scale2', () => deepStrictEqual(decodeQR(largeImage), largeText)); |
| 32 | +} |
| 33 | + |
| 34 | +main(); |
0 commit comments