|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Sustainable Web Design version 4 |
| 5 | + * |
| 6 | + * Updated calculations and figures from |
| 7 | + * https://sustainablewebdesign.org/estimating-digital-emissions/ |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +import { fileSize, SWDV4 } from "./constants/index.js"; |
| 12 | +import { formatNumber } from "./helpers/index.js"; |
| 13 | + |
| 14 | +const { |
| 15 | + OPERATIONAL_KWH_PER_GB_DATACENTER, |
| 16 | + OPERATIONAL_KWH_PER_GB_NETWORK, |
| 17 | + OPERATIONAL_KWH_PER_GB_DEVICE, |
| 18 | + EMBODIED_KWH_PER_GB_DATACENTER, |
| 19 | + EMBODIED_KWH_PER_GB_NETWORK, |
| 20 | + EMBODIED_KWH_PER_GB_DEVICE, |
| 21 | + GLOBAL_GRID_INTENSITY, |
| 22 | +} = SWDV4; |
| 23 | + |
| 24 | +class SustainableWebDesign { |
| 25 | + constructor(options) { |
| 26 | + this.options = options; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Calculate the operational energy of data transfer for each system segment |
| 31 | + * |
| 32 | + * @param {number} bytes |
| 33 | + * @returns {object} |
| 34 | + */ |
| 35 | + operationalEnergyPerSegment(bytes) { |
| 36 | + const transferedBytesToGb = bytes / fileSize.GIGABYTE; |
| 37 | + const dataCenter = transferedBytesToGb * OPERATIONAL_KWH_PER_GB_DATACENTER; |
| 38 | + const network = transferedBytesToGb * OPERATIONAL_KWH_PER_GB_NETWORK; |
| 39 | + const device = transferedBytesToGb * OPERATIONAL_KWH_PER_GB_DEVICE; |
| 40 | + |
| 41 | + return { |
| 42 | + dataCenter, |
| 43 | + network, |
| 44 | + device, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Calculate the operational emissions of data transfer for each system segment |
| 50 | + * |
| 51 | + * @param {number} bytes |
| 52 | + * @param {object} options |
| 53 | + * @returns {object} |
| 54 | + */ |
| 55 | + operationalEmissions(bytes, options = {}) { |
| 56 | + const { dataCenter, network, device } = |
| 57 | + this.operationalEnergyPerSegment(bytes); |
| 58 | + |
| 59 | + let dataCenterGridIntensity = GLOBAL_GRID_INTENSITY; |
| 60 | + let networkGridIntensity = GLOBAL_GRID_INTENSITY; |
| 61 | + let deviceGridIntensity = GLOBAL_GRID_INTENSITY; |
| 62 | + |
| 63 | + if (options?.gridIntensity) { |
| 64 | + const { device, network, dataCenter } = options.gridIntensity; |
| 65 | + |
| 66 | + if (device?.value || device?.value === 0) { |
| 67 | + deviceGridIntensity = device.value; |
| 68 | + } |
| 69 | + |
| 70 | + if (network?.value || network?.value === 0) { |
| 71 | + networkGridIntensity = network.value; |
| 72 | + } |
| 73 | + |
| 74 | + if (dataCenter?.value || dataCenter?.value === 0) { |
| 75 | + dataCenterGridIntensity = dataCenter.value; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const dataCenterEmissions = dataCenter * dataCenterGridIntensity; |
| 80 | + const networkEmissions = network * networkGridIntensity; |
| 81 | + const deviceEmissions = device * deviceGridIntensity; |
| 82 | + |
| 83 | + return { |
| 84 | + dataCenter: dataCenterEmissions, |
| 85 | + network: networkEmissions, |
| 86 | + device: deviceEmissions, |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Calculate the embodied energy of data transfer for each system segment |
| 92 | + * |
| 93 | + * @param {number} bytes |
| 94 | + * @returns {object} |
| 95 | + */ |
| 96 | + embodiedEnergyPerSegment(bytes) { |
| 97 | + const transferedBytesToGb = bytes / fileSize.GIGABYTE; |
| 98 | + const dataCenter = transferedBytesToGb * EMBODIED_KWH_PER_GB_DATACENTER; |
| 99 | + const network = transferedBytesToGb * EMBODIED_KWH_PER_GB_NETWORK; |
| 100 | + const device = transferedBytesToGb * EMBODIED_KWH_PER_GB_DEVICE; |
| 101 | + |
| 102 | + return { |
| 103 | + dataCenter, |
| 104 | + network, |
| 105 | + device, |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Calculate the embodied emissions of data transfer for each system segment |
| 111 | + * |
| 112 | + * @param {number} bytes |
| 113 | + * @returns {object} |
| 114 | + */ |
| 115 | + embodiedEmissions(bytes) { |
| 116 | + const { dataCenter, network, device } = |
| 117 | + this.embodiedEnergyPerSegment(bytes); |
| 118 | + |
| 119 | + const dataCenterGridIntensity = GLOBAL_GRID_INTENSITY; |
| 120 | + const networkGridIntensity = GLOBAL_GRID_INTENSITY; |
| 121 | + const deviceGridIntensity = GLOBAL_GRID_INTENSITY; |
| 122 | + |
| 123 | + // NOTE: Per the guidance in the SWDM v4, the grid intensity values for embodied emissions are fixed to the global grid intensity. |
| 124 | + |
| 125 | + const dataCenterEmissions = dataCenter * dataCenterGridIntensity; |
| 126 | + const networkEmissions = network * networkGridIntensity; |
| 127 | + const deviceEmissions = device * deviceGridIntensity; |
| 128 | + |
| 129 | + return { |
| 130 | + dataCenter: dataCenterEmissions, |
| 131 | + network: networkEmissions, |
| 132 | + device: deviceEmissions, |
| 133 | + }; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +export { SustainableWebDesign }; |
| 138 | +export default SustainableWebDesign; |
0 commit comments