|
| 1 | +/* |
| 2 | + * This benchmark is used to find the fastest way to discriminate |
| 3 | + * types of objects that are hidden under a symbol. This information |
| 4 | + * will be used to best discriminate TypeGPU resources. |
| 5 | + */ |
| 6 | + |
| 7 | +import { Bench } from 'tinybench'; |
| 8 | + |
| 9 | +const bench = new Bench({ |
| 10 | + name: 'discriminated union', |
| 11 | + time: 100, |
| 12 | + async setup() { |
| 13 | + // biome-ignore lint/suspicious/noExplicitAny: making sure GC has no impact on the results |
| 14 | + (globalThis as any).gc(); |
| 15 | + }, |
| 16 | +}); |
| 17 | + |
| 18 | +const $internal = Symbol('Internal functionality'); |
| 19 | + |
| 20 | +// biome-ignore format: Long array |
| 21 | +const STRING_TAGS = ['aaa', 'bbb', 'ccc'] as const; |
| 22 | +const NUMBER_TAGS = [0, 1, 2] as const; |
| 23 | +const NUMBER_TAG_CATALOG = { |
| 24 | + aaa: 0, |
| 25 | + bbb: 1, |
| 26 | + ccc: 2, |
| 27 | +} as const; |
| 28 | + |
| 29 | +const aaa = Symbol('aaa symbol'); |
| 30 | +const bbb = Symbol('bbb symbol'); |
| 31 | +const ccc = Symbol('ccc symbol'); |
| 32 | +const SYMBOL_TAGS = [aaa, bbb, ccc] as const; |
| 33 | + |
| 34 | +const NUMBER_OF_OBJS = 1000; |
| 35 | + |
| 36 | +let stringTaggedObjs: { |
| 37 | + [$internal]: { type: (typeof STRING_TAGS)[number] }; |
| 38 | +}[]; |
| 39 | +let numberTaggedObjs: { [$internal]: { type: (typeof NUMBER_TAGS)[number] } }[]; |
| 40 | +let symbolTaggedObjs: { [$internal]: { type: (typeof SYMBOL_TAGS)[number] } }[]; |
| 41 | +let symbolKeyedObjs: { [K in (typeof SYMBOL_TAGS)[number]]: boolean }[]; |
| 42 | + |
| 43 | +bench |
| 44 | + .add( |
| 45 | + 'string tags', |
| 46 | + () => { |
| 47 | + let count = 0; |
| 48 | + |
| 49 | + for (const obj of stringTaggedObjs) { |
| 50 | + if (obj[$internal].type === 'aaa') { |
| 51 | + count += 2; |
| 52 | + } else if (obj[$internal].type === 'bbb') { |
| 53 | + count += 3; |
| 54 | + } else if (obj[$internal].type === 'ccc') { |
| 55 | + count += 4; |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | + { |
| 60 | + beforeEach() { |
| 61 | + stringTaggedObjs = Array.from({ length: NUMBER_OF_OBJS }, () => ({ |
| 62 | + [$internal]: { |
| 63 | + // biome-ignore lint/style/noNonNullAssertion: in range |
| 64 | + type: STRING_TAGS[Math.floor(Math.random() * STRING_TAGS.length)]!, |
| 65 | + }, |
| 66 | + })); |
| 67 | + }, |
| 68 | + }, |
| 69 | + ) |
| 70 | + .add( |
| 71 | + 'number tags', |
| 72 | + async () => { |
| 73 | + let count = 0; |
| 74 | + |
| 75 | + for (const obj of numberTaggedObjs) { |
| 76 | + if (obj[$internal].type === NUMBER_TAG_CATALOG.aaa) { |
| 77 | + count += 2; |
| 78 | + } else if (obj[$internal].type === NUMBER_TAG_CATALOG.bbb) { |
| 79 | + count += 3; |
| 80 | + } else if (obj[$internal].type === NUMBER_TAG_CATALOG.ccc) { |
| 81 | + count += 4; |
| 82 | + } |
| 83 | + } |
| 84 | + }, |
| 85 | + { |
| 86 | + beforeEach() { |
| 87 | + numberTaggedObjs = Array.from({ length: NUMBER_OF_OBJS }, () => ({ |
| 88 | + [$internal]: { |
| 89 | + // biome-ignore lint/style/noNonNullAssertion: in range |
| 90 | + type: NUMBER_TAGS[Math.floor(Math.random() * NUMBER_TAGS.length)]!, |
| 91 | + }, |
| 92 | + })); |
| 93 | + }, |
| 94 | + }, |
| 95 | + ) |
| 96 | + .add( |
| 97 | + 'number tags (inlined catalog)', |
| 98 | + async () => { |
| 99 | + let count = 0; |
| 100 | + |
| 101 | + for (const obj of numberTaggedObjs) { |
| 102 | + if (obj[$internal].type === 0) { |
| 103 | + count += 2; |
| 104 | + } else if (obj[$internal].type === 1) { |
| 105 | + count += 3; |
| 106 | + } else if (obj[$internal].type === 2) { |
| 107 | + count += 4; |
| 108 | + } |
| 109 | + } |
| 110 | + }, |
| 111 | + { |
| 112 | + beforeEach() { |
| 113 | + numberTaggedObjs = Array.from({ length: NUMBER_OF_OBJS }, () => ({ |
| 114 | + [$internal]: { |
| 115 | + // biome-ignore lint/style/noNonNullAssertion: in range |
| 116 | + type: NUMBER_TAGS[Math.floor(Math.random() * NUMBER_TAGS.length)]!, |
| 117 | + }, |
| 118 | + })); |
| 119 | + }, |
| 120 | + }, |
| 121 | + ) |
| 122 | + .add( |
| 123 | + 'symbol tags', |
| 124 | + async () => { |
| 125 | + let count = 0; |
| 126 | + |
| 127 | + for (const obj of symbolTaggedObjs) { |
| 128 | + if (obj[$internal].type === aaa) { |
| 129 | + count += 2; |
| 130 | + } else if (obj[$internal].type === bbb) { |
| 131 | + count += 3; |
| 132 | + } else if (obj[$internal].type === ccc) { |
| 133 | + count += 4; |
| 134 | + } |
| 135 | + } |
| 136 | + }, |
| 137 | + { |
| 138 | + beforeEach() { |
| 139 | + symbolTaggedObjs = Array.from({ length: NUMBER_OF_OBJS }, () => ({ |
| 140 | + [$internal]: { |
| 141 | + // biome-ignore lint/style/noNonNullAssertion: in range |
| 142 | + type: SYMBOL_TAGS[Math.floor(Math.random() * SYMBOL_TAGS.length)]!, |
| 143 | + }, |
| 144 | + })); |
| 145 | + }, |
| 146 | + }, |
| 147 | + ) |
| 148 | + .add( |
| 149 | + 'symbol keys', |
| 150 | + async () => { |
| 151 | + let count = 0; |
| 152 | + |
| 153 | + for (const obj of symbolKeyedObjs) { |
| 154 | + if (obj[aaa]) { |
| 155 | + count += 2; |
| 156 | + } else if (obj[bbb]) { |
| 157 | + count += 3; |
| 158 | + } else if (obj[ccc]) { |
| 159 | + count += 4; |
| 160 | + } |
| 161 | + } |
| 162 | + }, |
| 163 | + { |
| 164 | + beforeEach() { |
| 165 | + symbolKeyedObjs = Array.from( |
| 166 | + { length: NUMBER_OF_OBJS }, |
| 167 | + () => |
| 168 | + ({ |
| 169 | + // biome-ignore lint/style/noNonNullAssertion: in range |
| 170 | + [SYMBOL_TAGS[Math.floor(Math.random() * SYMBOL_TAGS.length)]!]: |
| 171 | + true, |
| 172 | + }) as { [K in (typeof SYMBOL_TAGS)[number]]: boolean }, |
| 173 | + ); |
| 174 | + }, |
| 175 | + }, |
| 176 | + ); |
| 177 | + |
| 178 | +await bench.run(); |
| 179 | + |
| 180 | +console.log(bench.name); |
| 181 | +console.table(bench.table()); |
0 commit comments