Question: How can I optimize the usage of method sendItemIdsOverBroadcastChannel to reduce message size?
interface IItemId {
id: number;
classId: number;
}
interface IItem extends IItemId {
longString: string;
anotherLongString: string
}
interface IAnotherItem extends IItemId {
shortString: string;
anotherShortString: string
}
I have a method called sendItemIdsOverBroadcastChannel which takes an array of type IItemId. However, when lots of items are passed, the broadcast channel message becomes too large.
One approach is to map the IDs before adding them to the broadcast channel message:
sendItemIdsOveBroadcastChannel(itemIds: IItemId[]) {
const ids = itemIds.map(obj => { obj.id, obj.classId });
// add ids to broadcastchannel message
}
However, this results in iterating over the array and creating new objects every time, even if the method was called with just an array of IItemId and not extended types. Another potential solution is to map the array only when objects are of an extended type, but this may not be the most efficient or cleanest approach.
Are there any better solutions to optimize this process and reduce message size?