In childhood I was often told that sugar is a white death. Later, I realized that calories are calories, and those who talk about the dangers of sugar often simply do not own a materiel.
And suddenly it turned out that everything that adults were scaring me with is pure truth. Sugar is a terrible thing that kills the brain and slowly leads us to Alzheimer's. It can not be eaten by anyone and never. This topic is covered in detail in the book of Garry Taub Good Calories Bad Calories, as well as in the book of David Perlmutter Grain Brain.
But the article is not about that. I recently discovered that a group of my comrades are sitting tight on CoffeeScript. They are not ashamed of this fact and even manage to experience some kind of unnatural pleasure in the process. Moreover, they do not shy away from using the syntactic sugar contained in coffee, and even wanted to addict me to it.
')
Fortunately, I am endowed with a rare gift. I always recognize evil, and no matter what clothes it is dressed up this time.
The first dose is carefully (and absolutely free) offered on the official website. There, right on the start page, the code on the censcript and the javascript code are put into which the compiler will turn it.
We must pay tribute to people - they are not trying to deceive you. The code given on the start page is more than enough to permanently abandon syntactic sugar and, in general, think about it ten times before starting to use an overseas drink. I will not give the code in its entirety, especially since everyone can look at it on their own. I will give only a significant piece.
CoffeeScript
| Javascript
|
cubes = (math.cube num for num in list)
| cubes = (function() { var i, len, results; results = []; for (i = 0, len = list.length; i < len; i++) { num = list[i]; results.push(math.cube(num)); } return results; })();
|
list is an array, and math.cube, as you might guess, is a function that takes an input parameter to a cube.
Javascript code is nightmare
No one of course in his right mind and the bright memory of such a code would not have written itself. Behind this multi-letter is a simple and well-known construction.
cubes = list.map (math.cube);
Which, by the way, is clearer and shorter than the Coffeeman version.
What is the actual problem
Yes, from an aesthetic point of view, Javascript code is just awful, but when I hinted at it to my comrades, I was reasonably objected that it was the exhaust of the compiler that normal people do not read. On the contrary, you need to look at this code and be glad that you will never have to write it yourself.
The point of view is not new, repeatedly confirmed in practice by such monsters as, for example, Bjarne Stroustrup. If the compiler doesn’t mess up, then it’s really better to trust it, relax and have fun.
However, one glance at the above code is enough to realize that the compiler loses its mind at the sight of sugar. First of all, the post-crimped operator catches the eye, but of course it does not make the weather.
The real problem is that the code generated by the compiler is about 3 times slower than
cubes = list.map (math.cube); . Yes, quite right, having thoroughly thrown sugar, you can completely freeze a part of the code three times without getting anything in return.
How did that happen
The reason is simple and prosaic to ugliness. What is called arrays in javascript is called hashes in other programming languages, and the access time by key to the hash element is not the same as the access time to the array element by index. On the other hand, hash elements are linked into a list, so sequential enumeration of all elements can be done quickly and easily. Not as fast and not as simple as the elements of this array, but many times faster than the code generated by the CoffeeScript compiler does.
Maybe we actually measure different things.
As some probably already noticed -
cubes = list.map (math.cube); does the same thing as the code on a coffee script, but in fact is not its direct mapping. You can accurately display this code like this:
cubes = list.reduce(function(prev, curr) { return prev.concat(math.cube(curr)); }, []);
Maybe the fact is that a new array is returned to us every time? Maybe the whole thing is in
.push and that the array has to be grown? Well, that is, it is clear that this is impossible, but suddenly? Maybe so?
list.reduce(function(result, curr) { result.push(math.cube(curr)); return result; }, []);
This is generally a one-to-one mapping, all with the exception of busting taken from the original. But even such code is still 3 times faster than the code from the main page of CoffeeScript.
But you can be more truthful
Or maybe it’s not just access to the elements? Let's try to just sum up the values of the elements of the array, rather than shift them to a new array.
Compare this code:
var sum = (function(list) { var i, len, sum = 0; for (i = 0, len = list.length; i < len; i++) { var num = list[i]; sum += num; } return sum; }(list));
Here with this:
var sum = list.reduce(function(prev, curr) { return curr + prev; }, 0);
The results are not so straightforward. But the difference is still one and a half times in favor of javascript.
So it goes
From my point of view, if the technology
breaks from the foot right from the door
with such applications, this is a reason to think seriously before you start using it. Of course, if you indulge in CoffeeScript for a long time, and it pleases you - continue to indulge - coffee itself has not killed anyone yet. Especially considering that lambdas can be used in CoffeeScript without any difficulties and the compiler translates them into lambdas, which, as we have just found out, have no speed problems.
But with sugar, at least with some of its types, it is better to bind it - sugar is bad for ya.
PS
You can see the test code
here .
Update
The information presented in the article is valid for the node version v0.10.40. In the latest version of node and in the latest versions of browsers, the picture is
different . In Chrome version 45, the code generated by CoffeeScript is slower, it seems to be faster in fayfox. Although it is necessary to collect more statistics.