Static vs. dynamically calculated constants in code

March, 2021

In writing software, most of the time we need to write constants as a benchmark or reference value for which other values can be compared to or calculated from. Most of the time, we are tempted to calculate these constants on the fly (whenever they are required) so as to give our future selves or whoever that would be reading our code some context on how the constant value is being generated. For example, in order to express the number of seconds in a day, we could do something like:

const secondsInDay = 60 * 60 * 24;
console.log(secondsInDay)
// prints out: 86400


The above code gives whoever is reading the code instant meaning on how this constant value is being calculated i.e 60 seconds per minute * 60 minutes per hour * 24 hours.

However, there can be some performance and documentation improvements somewhere in there. Since this value is being calculated every time this section of your code is evaluated, some CPU time is being allocated to the arithmetic calculation and when aggregated over a number of CPU executions of your code, it could amount to some tangible CPU time being lost to this 'unneeded' arithmetic calculation. I've been obssessing over software performance of recent and I thought to myself that this can be improved.

I try to be empirical whenever I'm trying to reach a decision and so I did an experiment to check the time taken to evaluate that line of code above with respect to just writing the constant value without calculating it. This experiment was in JavaScript and it's very simple. The experiment can be found below:

console.time("duration");
const secondsInYear = 60 * 60 * 24 * 365;
console.timeEnd("duration");
// prints out the duration of the expression evaluation and variable assignment above


The second option I tested for which I will be advocating for moving forward is:

console.time("duration2");
const secondsInYear = 31536000 // 60secs * 60mins * 24hrs * 365days;
console.timeEnd("duration2")
// prints out the duration of the variable assignment above


I ran the above experiment multiple number of times and the second option was faster all the time since the CPU only had to do variable allocation. Also, the second option is more readable with the comment attached to the variable allocation expression. This little tweak in our constant variables allocation can help us shave off some CPU time that can be used to do other things in our program.
The first option entailed that the CPU would evaluate the expression on the right hand side and as well allocate the variable every time that line of code is passed. There is room for improvement in there.

In conclusion, when writing constants in your code, it's better you do the calculation beforehand and assign it to a variable while writing a comment or documentation to state how that constant value was arrived at. Static constant values are more performant and better documented than dynamically generated/calculated constants.