* Estimate the size of attributes in bytes. * * @param attributes - The attributes object to estimate the size of. * @returns The estimated size of the attributes in bytes.
(attributes: Record<string, unknown> | undefined)
| 1795 | * @returns The estimated size of the attributes in bytes. |
| 1796 | */ |
| 1797 | function estimateAttributesSizeInBytes(attributes: Record<string, unknown> | undefined): number { |
| 1798 | if (!attributes) { |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | let weight = 0; |
| 1803 | |
| 1804 | Object.values(attributes).forEach(value => { |
| 1805 | if (Array.isArray(value)) { |
| 1806 | weight += value.length * estimatePrimitiveSizeInBytes(value[0]); |
| 1807 | } else if (isPrimitive(value)) { |
| 1808 | weight += estimatePrimitiveSizeInBytes(value); |
| 1809 | } else { |
| 1810 | // For objects values, we estimate the size of the object as 100 bytes |
| 1811 | weight += 100; |
| 1812 | } |
| 1813 | }); |
| 1814 | |
| 1815 | return weight; |
| 1816 | } |
| 1817 | |
| 1818 | function estimatePrimitiveSizeInBytes(value: Primitive): number { |
| 1819 | if (typeof value === 'string') { |
no test coverage detected