|
| 1 | +import { jsonStringify, Logger } from '@graphql-hive/logger'; |
| 2 | +import { bench, describe } from 'vitest'; |
| 3 | + |
| 4 | +const voidlog = new Logger({ |
| 5 | + writers: [ |
| 6 | + { |
| 7 | + write() { |
| 8 | + // void |
| 9 | + }, |
| 10 | + }, |
| 11 | + ], |
| 12 | +}); |
| 13 | + |
| 14 | +describe.each([ |
| 15 | + { name: 'string' as const, value: 'hello' }, |
| 16 | + { name: 'integer' as const, value: 7 }, |
| 17 | + { name: 'float' as const, value: 7.77 }, |
| 18 | + { name: 'object' as const, value: { hello: 'world' } }, |
| 19 | +])('log formatting of $name', ({ name, value }) => { |
| 20 | + // we switch outside of the bench to avoid the overhead of the switch |
| 21 | + switch (name) { |
| 22 | + case 'string': |
| 23 | + bench('template literals', () => { |
| 24 | + voidlog.info(`hi there ${value}`); |
| 25 | + }); |
| 26 | + bench('interpolation', () => { |
| 27 | + voidlog.info('hi there %s', value); |
| 28 | + }); |
| 29 | + break; |
| 30 | + case 'integer': |
| 31 | + bench('template literals', () => { |
| 32 | + voidlog.info(`hi there ${value}`); |
| 33 | + }); |
| 34 | + bench('interpolation', () => { |
| 35 | + voidlog.info('hi there %i', value); |
| 36 | + }); |
| 37 | + break; |
| 38 | + case 'float': |
| 39 | + bench('template literals', () => { |
| 40 | + voidlog.info(`hi there ${value}`); |
| 41 | + }); |
| 42 | + bench('interpolation', () => { |
| 43 | + voidlog.info('hi there %d', value); |
| 44 | + }); |
| 45 | + break; |
| 46 | + case 'object': |
| 47 | + bench('template literals native stringify', () => { |
| 48 | + voidlog.info(`hi there ${JSON.stringify(value)}`); |
| 49 | + }); |
| 50 | + bench('template literals logger stringify', () => { |
| 51 | + voidlog.info(`hi there ${jsonStringify(value)}`); |
| 52 | + }); |
| 53 | + bench('interpolation', () => { |
| 54 | + voidlog.info('hi there %o', value); |
| 55 | + }); |
| 56 | + break; |
| 57 | + } |
| 58 | +}); |
0 commit comments