function* test() {
console.time("function-call")
loop();
console.timeEnd("function-call");
console.time("in-function");
var i, j;
i = 0;
while (i < 10000) {
j = 0;
while (j < 10000) {
j++
}
i++;
}
console.timeEnd("in-function");
}
function loop() {
var i, j;
i = 0;
while (i < 10000) {
j = 0;
while (j < 10000) {
j++
}
i++;
}
}
test().next();
I copied the code block in the 'loop', and pasted into 'test' function to compare time.
function-call: 84ms
in-function: 596ms
calling a function is much faster than looping inside the function. Why is that so?
@Cristian Traìna Node does not allow to skip empty loops. These optimizations are allowed only in compiled languages like Pascal or C/C++ with flags like
-O2.For this program
We can get the following dependency of time of execution from a number of loops. It is a LogLog chart. The first flat area it domain when dominating part of execution time is starting of NodeJs. After 1M loops, you can see that increasing number of iteration scaling with time linearly. Highest measurement takes about 1000 seconds so definitely, V8 does not skip the empty loop.
(source: gustawdaniel.pl)
Coming back to script from question:
The computer of my Friend tests:
So now it seems to be a problem with Firefox and Edge.
Any of these browsers except Firefox and Edge uses V8 engine. Firefox is described here:
and uses
Quantum Flow:.Edge team think about adopting Chromium engine
From this article
We can see that only
Chromium from GoogleandGecko Quantum from Mozillawill be supported in the future.If anyone has access to Safari or Edge, please append tests.