To all: This has more the feel of a "comment". Acknowledged. I chose to use the space of an "answer". Please tolerate.
@StefanoFratini: Please take my note as building on your work. I want to avoid being critical.
Here are two ways to further improve the code in your post:
- Use both halves of the tuple coming from process.hrtime(). It returns an array [seconds, nanoseconds]. Your code uses the nanoseconds part of the tuple (element 1) and I can't find that it uses the seconds part (element 0).
- Be explicit about units.
Can I match my bluster? Dunno. Here's a development of Stephano's code. It has flaws; I won't be surprised if someone tells me about it. And that'd be okay.
"use strict";
var a = function(val) { return val+1; }
var b = function(val) { return val-1; }
var c = function(val) { return val*2 }
var time = process.hrtime();
var reps = 100000000
for(var i = 0; i < reps; i++) { a(b(c(100))); }
time = process.hrtime(time)
let timeWith = time[0] + time[1]/1000000000
console.log(`Elapsed time with function calls: ${ timeWith } seconds`);
time = process.hrtime();
var tmp;
for(var i = 0; i < reps; i++) { tmp = 100*2 - 1 + 1; }
time = process.hrtime(time)
let timeWithout = time[0] + time[1]/1000000000
console.log(`Elapsed time without function calls: ${ timeWithout } seconds`);
let percentWith = 100 * timeWith / timeWithout
console.log(`\nThe time with function calls is ${ percentWith } percent\n` +
`of time without function calls.`)
console.log(`\nEach repetition with a function call used roughly ` +
`${ timeWith / reps } seconds.` +
`\nEach repetition without a function call used roughly ` +
`${ timeWithout / reps } seconds.`)
It is clearly a descendent of Stephano's code. The results are quite different.
Elapsed time with function calls: 4.671479346 seconds
Elapsed time without function calls: 0.503176535 seconds
The time with function calls is 928.397693664312 percent
of time without function calls.
Each repetition with a function call used roughly 4.671479346e-8 seconds.
Each repetition without a function call used roughly 5.0317653500000005e-9 seconds.
Like Stephano, I used Win10 and Node (v6.2.0 for me).
I acknowledge the arguments that
- "For perspective, in a nanosecond (a billionth, 1e-9), light travels roughly 12 inches."
- "We're only talking about small numbers of nanoseconds (47 to 5), so who cares about percentages?"
- "Some algorithms make zillions of function calls each second, so it adds up for them."
- "Most of us developers don't work with those algorithms, so worrying about the number of function calls is counterproductive for most of us."
I'll hang my hat on the economic argument: My computer and the one before it each cost less than $400 (US). If a software engineer earns something like $90 to $130 per hour, the value of their time to their bosses is at a ratio of one computer like mine to three or four hours of their work. In that environment:
How does that compare to the dollars per hour a company loses when software it needs stops working?
How does that compare to lost good will and prestige when a paying customer temporarily can't use shrink-wrapped software produced by a business partner?
There many other such questions. I'll omit them.
As I interpret the answers: Readability and maintainability reign over computer performance. My advice? Write the first version of your code accordingly. Many people I respect say short functions help.
Once you finish your code and don't like the performance, find the choke points. Many people I respect say those points are never where you would have expected them. Work 'em when you know 'em.
So both sides are right. Some.
Me? I'll guess I'm off somewhere. Two cents.