-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Description
- Version: 8 and above
- Platform: windows
- Subsystem: 10-pro
Dear noders.
There's a hands-on demo I give every now and then, which shows why putting everything in try{}catch(e){} isn't a good idea. I usually give it to C# or Java developers that get a taste of node, and bring with them their old customs with them. It goes as follows.
Consider the following two files:
no-try.js
var i = 0;
function foo() {
if ( 0 == ++i % 100) console.log('depth - ', i);
foo();
}
foo()
with-try.js
var i = 0;
function foo() {
try {
if ( 0 == ++i % 100) console.log('depth - ', i);
foo();
} catch(e) {}
}
foo()
The test tries to find how many call-stack the code will enter before it crashes, and shows that the try-catch impacts significantly the call-stack depth.
I've been showing this demo ever since the ealy days of node. I did not get a class for a while, and now that I did - I started to show them the findings, but was so dismayed by the results - I had to open an issue about it and ask your opinion here.
Here are my results on this current workplace laptop, which is a dell M2800, i7vPro, 16G, bla bla.
+--------+-------+--------+
|node -v | no-try|with-try|
+--------+-------+--------+
|v0.6.21 | 18400 | 11400 |
|v0.8.28 | 20900 | 11400 |
|v0.10.48| 20900 | 11400 |
|v0.12.18| 20700 | 11200 |
|v4.9.1 | 20600 | 17700 |
|v6.14.2 | 20600 | 17700 |
|v8.9.4 | 8200 | 7600 |
|v10.3.0 | 10800 | 9900 |
+--------+-------+--------+
Friends - I was dancing with joy when node 4.4 got to 20K depth, and was even happier when I saw you fond how to make these commonly abused try blocks was less significant.
But while you can still see the footprint of try blocks - in versions above 8 - the overall depth is much more limited...
Is that on purpose? Is there some inner mechanism that limits harder, or that these versions use so much more memory even with such a vanilla code?