Skip to content

Commit 2d19163

Browse files
authored
perf(Components): Hash table (#10893)
perf(Components): hash table
1 parent 9bca4af commit 2d19163

File tree

1 file changed

+31
-62
lines changed

1 file changed

+31
-62
lines changed

packages/discord.js/src/util/Components.js

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -141,44 +141,7 @@ const { ComponentType } = require('discord-api-types/v10');
141141
* @ignore
142142
*/
143143
function createComponent(data) {
144-
if (data instanceof Component) {
145-
return data;
146-
}
147-
148-
switch (data.type) {
149-
case ComponentType.ActionRow:
150-
return new ActionRow(data);
151-
case ComponentType.Button:
152-
return new ButtonComponent(data);
153-
case ComponentType.StringSelect:
154-
return new StringSelectMenuComponent(data);
155-
case ComponentType.TextInput:
156-
return new TextInputComponent(data);
157-
case ComponentType.UserSelect:
158-
return new UserSelectMenuComponent(data);
159-
case ComponentType.RoleSelect:
160-
return new RoleSelectMenuComponent(data);
161-
case ComponentType.MentionableSelect:
162-
return new MentionableSelectMenuComponent(data);
163-
case ComponentType.ChannelSelect:
164-
return new ChannelSelectMenuComponent(data);
165-
case ComponentType.Container:
166-
return new ContainerComponent(data);
167-
case ComponentType.TextDisplay:
168-
return new TextDisplayComponent(data);
169-
case ComponentType.File:
170-
return new FileComponent(data);
171-
case ComponentType.MediaGallery:
172-
return new MediaGalleryComponent(data);
173-
case ComponentType.Section:
174-
return new SectionComponent(data);
175-
case ComponentType.Separator:
176-
return new SeparatorComponent(data);
177-
case ComponentType.Thumbnail:
178-
return new ThumbnailComponent(data);
179-
default:
180-
return new Component(data);
181-
}
144+
return data instanceof Component ? data : new (ComponentTypeToComponent[data.type] ?? Component)(data);
182145
}
183146

184147
/**
@@ -188,30 +151,7 @@ function createComponent(data) {
188151
* @ignore
189152
*/
190153
function createComponentBuilder(data) {
191-
if (data instanceof ComponentBuilder) {
192-
return data;
193-
}
194-
195-
switch (data.type) {
196-
case ComponentType.ActionRow:
197-
return new ActionRowBuilder(data);
198-
case ComponentType.Button:
199-
return new ButtonBuilder(data);
200-
case ComponentType.StringSelect:
201-
return new StringSelectMenuBuilder(data);
202-
case ComponentType.TextInput:
203-
return new TextInputBuilder(data);
204-
case ComponentType.UserSelect:
205-
return new UserSelectMenuBuilder(data);
206-
case ComponentType.RoleSelect:
207-
return new RoleSelectMenuBuilder(data);
208-
case ComponentType.MentionableSelect:
209-
return new MentionableSelectMenuBuilder(data);
210-
case ComponentType.ChannelSelect:
211-
return new ChannelSelectMenuBuilder(data);
212-
default:
213-
return new ComponentBuilder(data);
214-
}
154+
return data instanceof ComponentBuilder ? data : new (ComponentTypeToBuilder[data.type] ?? ComponentBuilder)(data);
215155
}
216156

217157
/**
@@ -274,3 +214,32 @@ const TextInputComponent = require('../structures/TextInputComponent');
274214
const ThumbnailComponent = require('../structures/ThumbnailComponent');
275215
const UserSelectMenuBuilder = require('../structures/UserSelectMenuBuilder');
276216
const UserSelectMenuComponent = require('../structures/UserSelectMenuComponent');
217+
218+
const ComponentTypeToComponent = {
219+
[ComponentType.ActionRow]: ActionRow,
220+
[ComponentType.Button]: ButtonComponent,
221+
[ComponentType.StringSelect]: StringSelectMenuComponent,
222+
[ComponentType.TextInput]: TextInputComponent,
223+
[ComponentType.UserSelect]: UserSelectMenuComponent,
224+
[ComponentType.RoleSelect]: RoleSelectMenuComponent,
225+
[ComponentType.MentionableSelect]: MentionableSelectMenuComponent,
226+
[ComponentType.ChannelSelect]: ChannelSelectMenuComponent,
227+
[ComponentType.Container]: ContainerComponent,
228+
[ComponentType.TextDisplay]: TextDisplayComponent,
229+
[ComponentType.File]: FileComponent,
230+
[ComponentType.MediaGallery]: MediaGalleryComponent,
231+
[ComponentType.Section]: SectionComponent,
232+
[ComponentType.Separator]: SeparatorComponent,
233+
[ComponentType.Thumbnail]: ThumbnailComponent,
234+
};
235+
236+
const ComponentTypeToBuilder = {
237+
[ComponentType.ActionRow]: ActionRowBuilder,
238+
[ComponentType.Button]: ButtonBuilder,
239+
[ComponentType.StringSelect]: StringSelectMenuBuilder,
240+
[ComponentType.TextInput]: TextInputBuilder,
241+
[ComponentType.UserSelect]: UserSelectMenuBuilder,
242+
[ComponentType.RoleSelect]: RoleSelectMenuBuilder,
243+
[ComponentType.MentionableSelect]: MentionableSelectMenuBuilder,
244+
[ComponentType.ChannelSelect]: ChannelSelectMenuBuilder,
245+
};

0 commit comments

Comments
 (0)