<!--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 ?? '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.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 | 1237974.1 Ops/sec |
test two loop | 1822530.8 Ops/sec |
The benchmark measures the performance of two different approaches to processing a dataset named consommations
, which represents monthly consumption data over a series of months and years. The focus of the benchmark is on how efficiently the JavaScript code handles this data, specifically through looping constructs.
Test on Loop for Each
forEach
method to iterate over the consommations
array and accumulate the results based on the current year and month. The results are computed by a series of helpers to merge data as necessary for the state of the consumption.Test Two Loop (Use of map
method)
map
method to transform the consommations
array into a new array that includes the state and processed consumption data. forEach
followed by pushing to a new array.map
method creates an entirely new array, which can be memory-intensive.forEach
which can make it harder to grasp for developers not familiar with functional programming paradigms.undefined
.null
or undefined
.ExecutionsPerSecond
metric indicating how many iterations of the benchmark can be executed in a second. The test that employs map
yields a higher execution rate than forEach
, suggesting it is generally more efficient for this particular processing task.for
loop can be an alternative, providing maximum control and often better performance for larger datasets since it eliminates the overhead of callback functions.reduce
method could also be employed to achieve similar results, allowing for accumulation of results in a single pass through the array.In conclusion, the benchmark compares the efficiency of two different looping constructs in processing consumption data. The findings suggest trade-offs in performance and readability that should be weighed depending on the specific use case, dataset size, and development context.