<!--your preparation HTML code goes here-->
const consommations = [{"annee":2023,"mois":2,"fiabilite_conso":"A"},{"annee":2023,"mois":3,"fiabilite_conso":"E","consommation":{"litre":19624,"m3":19.624,"prix_m3_estime":64.37},"index":{"litre":553554,"m3":553.554}},{"annee":2023,"mois":4,"fiabilite_conso":"M","consommation":{"litre":20380,"m3":20.38,"prix_m3_estime":66.85},"index":{"litre":573178,"m3":573.178}},{"annee":2023,"mois":5,"fiabilite_conso":"M","consommation":{"litre":19293,"m3":19.293,"prix_m3_estime":63.28},"index":{"litre":593558,"m3":593.558}},{"annee":2023,"mois":6,"fiabilite_conso":"M","consommation":{"litre":20048,"m3":20.048,"prix_m3_estime":65.76},"index":{"litre":612851,"m3":612.851}},{"annee":2023,"mois":7,"fiabilite_conso":"E","consommation":{"litre":22635,"m3":22.635,"prix_m3_estime":74.24},"index":{"litre":632899,"m3":632.899}},{"annee":2023,"mois":8,"fiabilite_conso":"M","consommation":{"litre":23212,"m3":23.212,"prix_m3_estime":76.14},"index":{"litre":655534,"m3":655.534}},{"annee":2023,"mois":9,"fiabilite_conso":"M","consommation":{"litre":18667,"m3":18.667,"prix_m3_estime":61.23},"index":{"litre":678746,"m3":678.746}},{"annee":2023,"mois":10,"fiabilite_conso":"E","consommation":{"litre":3319,"m3":3.319,"prix_m3_estime":10.89},"index":{"litre":697413,"m3":697.413}},{"annee":2023,"mois":11,"fiabilite_conso":"M","consommation":{"litre":481,"m3":0.481,"prix_m3_estime":1.58},"index":{"litre":700732,"m3":700.732}},{"annee":2023,"mois":12,"fiabilite_conso":"M","consommation":{"litre":1418,"m3":1.418,"prix_m3_estime":4.65},"index":{"litre":701213,"m3":701.213}},{"annee":2024,"mois":1,"fiabilite_conso":"M","consommation":{"litre":2283,"m3":2.283,"prix_m3_estime":7.49},"index":{"litre":702631,"m3":702.631}},{"annee":2024,"mois":2,"fiabilite_conso":"M","consommation":{"litre":4054,"m3":4.054,"prix_m3_estime":13.3},"index":{"litre":704914,"m3":704.914}},{"annee":2024,"mois":3,"fiabilite_conso":"E","consommation":{"litre":7894,"m3":7.894,"prix_m3_estime":25.89},"index":{"litre":708968,"m3":708.968}},{"annee":2024,"mois":4,"fiabilite_conso":"M","consommation":{"litre":9116,"m3":9.116,"prix_m3_estime":29.9},"index":{"litre":716862,"m3":716.862}},{"annee":2024,"mois":5,"fiabilite_conso":"E","consommation":{"litre":9993,"m3":9.993,"prix_m3_estime":32.78},"index":{"litre":725978,"m3":725.978}},{"annee":2024,"mois":6,"fiabilite_conso":"E","consommation":{"litre":5710,"m3":5.71,"prix_m3_estime":18.73},"index":{"litre":735971,"m3":735.971}},{"annee":2024,"mois":6,"fiabilite_conso":"E","consommation":{"litre":972,"m3":0.972,"prix_m3_estime":3.19},"index":{"litre":619,"m3":0.619}},{"annee":2024,"mois":7,"fiabilite_conso":"E","consommation":{"litre":7604,"m3":7.604,"prix_m3_estime":24.94},"index":{"litre":1591,"m3":1.591}},{"annee":2024,"mois":8,"fiabilite_conso":"M","consommation":{"litre":8279,"m3":8.279,"prix_m3_estime":27.16},"index":{"litre":9195,"m3":9.195}},{"annee":2024,"mois":9,"fiabilite_conso":"M","consommation":{"litre":7006,"m3":7.006,"prix_m3_estime":22.98},"index":{"litre":17474,"m3":17.474}},{"annee":2024,"mois":10,"fiabilite_conso":"E","consommation":{"litre":6036,"m3":6.036,"prix_m3_estime":19.8},"index":{"litre":24480,"m3":24.48}},{"annee":2024,"mois":11,"fiabilite_conso":"A"},{"annee":2024,"mois":12,"fiabilite_conso":"A"},{"annee":2025,"mois":1,"fiabilite_conso":"A"},{"annee":2025,"mois":2,"fiabilite_conso":"A"}]
const getConsommationObjectDate = (
annee,
mois,
jour,
) => ({
annee,
mois,
jour,
});
const consommationIndisponibleObjectData = {state: 'INDISPONIBLE'}
const fusionState = (
actual,
added,
) =>
added === 'A'
? actual ?? 'INDISPONIBLE'
: actual === 'ESTIMEE' || added === 'E'
? 'ESTIMEE'
: 'MESUREE';
const fusionConsommationObjectData = (
actual,
added,
) => ({
volume: (actual?.volume ?? 0) + (added?.litre ?? 0),
prix:
actual?.prix || added?.prix_m3_estime
? (actual?.prix ?? 0) + (added?.prix_m3_estime ?? 0)
: undefined,
});
const fusionData = (
actual,
added,
) => ({
state: fusionState(actual.state, added.fiabilite_conso),
consommation:
actual.consommation || added.consommation
? fusionConsommationObjectData(actual.consommation, added.consommation)
: undefined,
ecoulement:
actual.ecoulement || added.ecoulement
? fusionConsommationObjectData(actual.ecoulement, added.ecoulement)
: undefined,
unit: 'L',
});
const result= [];
let currentYear;
let currentMonth;
let currentData = {state: 'INDISPONIBLE'};
consommations.forEach((conso, index) => {
if (index === 0) {
currentYear = conso.annee;
currentMonth = conso.mois;
currentData = fusionData(consommationIndisponibleObjectData, conso);
} else if (conso.annee === currentYear && conso.mois === currentMonth) {
currentData = fusionData(currentData, conso);
} else {
result.push({
date: getConsommationObjectDate(currentYear, currentMonth - 1),
data: currentData,
});
currentYear = conso.annee;
currentMonth = conso.mois;
currentData = fusionData(consommationIndisponibleObjectData, conso);
}
if (index === consommations.length - 1) {
result.push({
date: getConsommationObjectDate(currentYear, currentMonth - 1),
data: currentData,
});
}
});
const getConsommationObjectDate = (
annee,
mois,
jour,
) => ({
annee,
mois,
jour,
});
const getConsommationObjectData = (
consommation,
) =>
consommation
? {
volume: consommation.litre,
prix: consommation?.prix_m3_estime,
}
: undefined;
const getConsommationMensuelleObjectData = (
consommation,
) => ({
state:
consommation.fiabilite_conso === 'M'
? 'MESUREE'
: 'ESTIMEE',
consommation: getConsommationObjectData(consommation.consommation),
ecoulement: getConsommationObjectData(consommation.ecoulement),
unit: 'L',
});
const consommationIndisponibleObjectData = {state: 'INDISPONIBLE'}
const fusionState = (
actual,
added,
) =>
added === 'A'
? actual
: actual === 'E' || added === 'E'
? 'E'
: 'M';
const fusionConsommationObjectData = (
actual,
added,
) => ({
litre: (actual?.litre ?? 0) + (added?.litre ?? 0),
prix_m3_estime:
actual?.prix_m3_estime || added?.prix_m3_estime
? (actual?.prix_m3_estime ?? 0) + (added?.prix_m3_estime ?? 0)
: undefined,
});
const fusionData = (
actual,
added,
) => ({
actual,
fiabilite_conso: fusionState(actual.fiabilite_conso, added.fiabilite_conso),
consommation:
actual.consommation || added.consommation
? fusionConsommationObjectData(actual.consommation, added.consommation)
: undefined,
ecoulement:
actual.ecoulement || added.ecoulement
? fusionConsommationObjectData(actual.ecoulement, added.ecoulement)
: undefined,
});
const result= [];
let currentYear;
let currentMonth;
let currentData;
consommations.forEach((conso, index) => {
if (index === 0) {
currentYear = conso.annee;
currentMonth = conso.mois;
currentData = conso;
} else if (conso.annee === currentYear && conso.mois === currentMonth) {
currentData = fusionData(currentData, conso);
} else {
result.push(currentData);
currentYear = conso.annee;
currentMonth = conso.mois;
currentData = conso;
}
if (index === consommations.length - 1) {
result.push(currentData);
}
});
result.map(conso => ({
date: getConsommationObjectDate(conso.annee, conso.mois - 1),
data:
conso.fiabilite_conso === 'A'
? {state: 'INDISPONIBLE'}
: getConsommationMensuelleObjectData(conso),
}))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
test on loop for each | |
test two loop |
Test name | Executions per second |
---|---|
test on loop for each | 1199979.4 Ops/sec |
test two loop | 1085761.0 Ops/sec |
Conso duplicates solution 2
This benchmark is concerned with testing the performance of two different coding approaches that handle the manipulation and consolidation of consumption data represented in a specific format. The consomation data contains monthly consumption figures for various years, indicating the reliability of the consumption measurements with several attributes.
Test Case 1: test on loop for each
This method processes the consommations
array with a single loop where it checks if the current entry belongs to the same year and month as the previous one. If it does, the data is combined; otherwise, it creates an entry in the result array and resets the data for the new month.
Pros:
Cons:
Test Case 2: test two loop
This approach executes two loops: the first one aggregates consumption data, while the second one transforms consolidated entries into a final structure that includes dates.
Pros:
Cons:
consommations
array twice, which could lead to a performance hit especially with a large dataset.test on loop for each
: 1,199,979 executions/secondtest two loop
: 1,085,761 executions/secondFrom the benchmark results, the first method (test on loop for each
) performs significantly better than the second method in terms of raw execution speed.
There are no special JavaScript libraries or advanced JavaScript syntax being employed in the benchmark cases. The JavaScript language features utilized include:
?.
): This allows for safe access to nested object properties, returning undefined
instead of throwing an error if a property is undefined
or null
.Map and Reduce: Instead of manually managing states within loops, the Array.prototype.map
and Array.prototype.reduce
methods could be applied for a more functional approach to data transformations and consolidations. This may lead to cleaner code, albeit at the cost of performance.
Libraries like Lodash: Utilizing utility libraries can greatly simplify operations like merging objects, deep cloning, and data manipulation while improving code readability.
Typed Arrays: If the data structure were large, using typed arrays could provide a performance enhancement by leveraging the lower-level memory storage structure of uniform types.
Web Workers: For particularly heavy computations or larger datasets, offloading operations to Web Workers could improve responsiveness if you're working within a browser environment.
This benchmark underscores the importance of method selection in coding practices to achieve better performance and maintainability. While the first approach demonstrates superior performance, the second method's clearer structure may ultimately be advantageous for teams prioritizing code maintainability over raw execution speed. Software engineers should weigh these factors according to their specific project needs and constraints.