|
| 1 | +import { bench } from '@ark/attest' |
| 2 | +import type { DB } from '../typings/test-d/huge-db.test-d.js' |
| 3 | +import type { Kysely } from '../../dist/index.js' |
| 4 | + |
| 5 | +// `insert-into.bench.ts` only covers picking the table. These cover the parts |
| 6 | +// that carry data - the values themselves, upserts and `set` - which is where a |
| 7 | +// row type gets matched against the table's insertable columns. Bulk inserts |
| 8 | +// are worth watching in particular: the cost should stay flat in the number of |
| 9 | +// rows, since every row is checked against the same type. |
| 10 | + |
| 11 | +declare const kysely: Kysely<DB> |
| 12 | +declare const kyselyAny: Kysely<any> |
| 13 | + |
| 14 | +const row = { |
| 15 | + col_1d726898491fbca9a8dac855d2be1be8: 1, |
| 16 | + col_2e66a5c7e24d1d066230f368ce8b094e: 'a', |
| 17 | + col_2f76db193eac6ad0f152563313673ac9: new Date(), |
| 18 | + col_4f84013a8b5e4c2b7529058c8fafcaa8: 'b', |
| 19 | + col_d2508118d0d39e198d1129d87d692d59: new Date(), |
| 20 | + col_454ff479a3b5a9ef082d9be9ac02a6f4: 'c', |
| 21 | + col_3917508388f24a50271f7088b657123c: 'd', |
| 22 | +} |
| 23 | + |
| 24 | +declare const rows: (typeof row)[] |
| 25 | + |
| 26 | +console.log('insert-values.bench.ts:\n') |
| 27 | + |
| 28 | +bench.baseline(() => { |
| 29 | + return kysely.insertInto('my_table') |
| 30 | +}) |
| 31 | + |
| 32 | +bench('kysely.insertInto(table).values(row)', () => { |
| 33 | + return kysely.insertInto('my_table').values(row) |
| 34 | +}).types([1038, 'instantiations']) |
| 35 | + |
| 36 | +bench('kysely.insertInto(table).values(~row)', () => { |
| 37 | + // @ts-expect-error |
| 38 | + return kysely.insertInto('my_table').values({ ...row, no_such_column: 1 }) |
| 39 | +}).types([1171, 'instantiations']) |
| 40 | + |
| 41 | +bench('kysely.insertInto(table).values([row])', () => { |
| 42 | + return kysely.insertInto('my_table').values([row]) |
| 43 | +}).types([1047, 'instantiations']) |
| 44 | + |
| 45 | +bench('kysely.insertInto(table).values([row x5])', () => { |
| 46 | + return kysely.insertInto('my_table').values([row, row, row, row, row]) |
| 47 | +}).types([1047, 'instantiations']) |
| 48 | + |
| 49 | +bench('kysely.insertInto(table).values([row x10])', () => { |
| 50 | + return kysely |
| 51 | + .insertInto('my_table') |
| 52 | + .values([row, row, row, row, row, row, row, row, row, row]) |
| 53 | +}).types([1047, 'instantiations']) |
| 54 | + |
| 55 | +bench('kysely.insertInto(table).values(row[])', () => { |
| 56 | + return kysely.insertInto('my_table').values(rows) |
| 57 | +}).types([1045, 'instantiations']) |
| 58 | + |
| 59 | +bench('kysely.insertInto(table).values(eb => row)', () => { |
| 60 | + return kysely.insertInto('my_table').values((eb) => ({ |
| 61 | + ...row, |
| 62 | + col_1d726898491fbca9a8dac855d2be1be8: eb |
| 63 | + .selectFrom('my_table as t2') |
| 64 | + .select('t2.col_1d726898491fbca9a8dac855d2be1be8') |
| 65 | + .limit(1), |
| 66 | + })) |
| 67 | +}).types([2522, 'instantiations']) |
| 68 | + |
| 69 | +bench('kysely..onConflict(oc => oc.column(column).doNothing())', () => { |
| 70 | + return kysely |
| 71 | + .insertInto('my_table') |
| 72 | + .values(row) |
| 73 | + .onConflict((oc) => |
| 74 | + oc.column('col_1d726898491fbca9a8dac855d2be1be8').doNothing(), |
| 75 | + ) |
| 76 | +}).types([1102, 'instantiations']) |
| 77 | + |
| 78 | +bench('kysely..onConflict(oc => oc.column(column).doUpdateSet(row))', () => { |
| 79 | + return kysely |
| 80 | + .insertInto('my_table') |
| 81 | + .values(row) |
| 82 | + .onConflict((oc) => |
| 83 | + oc.column('col_1d726898491fbca9a8dac855d2be1be8').doUpdateSet(row), |
| 84 | + ) |
| 85 | +}).types([2131, 'instantiations']) |
| 86 | + |
| 87 | +bench('kysely..onConflict(oc => oc..doUpdateSet(eb => excluded ref))', () => { |
| 88 | + return kysely |
| 89 | + .insertInto('my_table') |
| 90 | + .values(row) |
| 91 | + .onConflict((oc) => |
| 92 | + oc.column('col_1d726898491fbca9a8dac855d2be1be8').doUpdateSet({ |
| 93 | + col_2e66a5c7e24d1d066230f368ce8b094e: (eb) => |
| 94 | + eb.ref('excluded.col_2e66a5c7e24d1d066230f368ce8b094e'), |
| 95 | + }), |
| 96 | + ) |
| 97 | +}).types([4458, 'instantiations']) |
| 98 | + |
| 99 | +bench('kysely.updateTable(table).set(row)', () => { |
| 100 | + return kysely.updateTable('my_table').set(row) |
| 101 | +}).types([848, 'instantiations']) |
| 102 | + |
| 103 | +bench('kysely.updateTable(table).set(column, value)', () => { |
| 104 | + return kysely |
| 105 | + .updateTable('my_table') |
| 106 | + .set('my_table.col_2e66a5c7e24d1d066230f368ce8b094e', 'x') |
| 107 | +}).types([3078, 'instantiations']) |
| 108 | + |
| 109 | +bench('kysely.updateTable(table).set(eb => row)', () => { |
| 110 | + return kysely.updateTable('my_table').set((eb) => ({ |
| 111 | + col_1d726898491fbca9a8dac855d2be1be8: eb( |
| 112 | + 'col_1d726898491fbca9a8dac855d2be1be8', |
| 113 | + '+', |
| 114 | + 1, |
| 115 | + ), |
| 116 | + })) |
| 117 | +}).types([3520, 'instantiations']) |
| 118 | + |
| 119 | +bench('kyselyAny.insertInto(table).values(row)', () => { |
| 120 | + return kyselyAny.insertInto('my_table').values(row) |
| 121 | +}).types([421, 'instantiations']) |
| 122 | + |
| 123 | +bench('kyselyAny.updateTable(table).set(row)', () => { |
| 124 | + return kyselyAny.updateTable('my_table').set(row) |
| 125 | +}).types([333, 'instantiations']) |
0 commit comments