📜 ⬆️ ⬇️

How much is Node.js faster than Windows Script Host? And what does this tell us?

[quote] A reader who visits NodeJS.org website sees four quotes from site builders in the center of the page expressing pleasure from Node. Quotes change every minute. With some luck (or patience when reloading the page), the reader will see praise from Claudio Caldato of Microsoft Open Technologies, Inc. - for your convenience, I quote this quote in the illustration to the right.

What caused such praise? After all, it would seem, long before the Node engine came to life at all, Microsoft had its own (built in Windows, starting with Windows 98, and also installed with Internet Explorer 5) a tool for running scripts - Windows Script Host .

The answer to this question is obvious: Node.js runs much faster. (He, by the way, also has a simpler API. But the main thing is speed.)
')
But how much faster is Node than WSH?

It is not difficult to find out empirically. Let's take that script, with which evgeniyup yesterday compared the speed of WSH with the speed of the virtual machine of its ObjectScript language. At the beginning of the script, add a dozen lines - a “crutch” for Node.js, which implements the WSH function WScript.Echo in its absence.

show (or re-hide) the source code of the script
// CScript to Node.js: if (typeof WScript == "undefined") WScript = {}; if (typeof WScript.Echo == "undefined") WScript.Echo = function(){ var i; var log = ''; for (i=0; i < arguments.length; i++){ log += arguments[i]; } console.log(log); } var fannkuch = function(n) { var p = [], q = [], s = [], sign = 1, maxflips = 0, sum = 0; var i; for(i=1; i<=n; i++) p[i] = q[i] = s[i] = i; for(;;){ // Copy and flip. var q1 = p[1]; // Cache 1st element. if(q1 != 1){ for(i=2; i<=n; i++) q[i] = p[i]; // Work on a copy. var flips = 1; for(;;){ var qq = q[q1]; if(qq == 1){ // ... until 1st element is 1. sum = sum + sign*flips; if(flips > maxflips){ maxflips = flips; } // New maximum? break; } q[q1] = q1; if(q1 >= 4){ var i = 2, j = q1 - 1 for(;;){ var tmp = q[i]; q[i] = q[j]; q[j] = tmp; if(++i >= --j) break; } } q1 = qq; flips++; } } // Permute. if(sign == 1){ var tmp = p[2]; p[2] = p[1]; p[1] = tmp; sign = -1; // Rotate 1<-2. }else{ var tmp = p[2]; p[2] = p[3]; p[3] = tmp; sign = 1; // Rotate 1<-2 and 1<-2<-3. for(i = 3;; i++){ // print "mark 4" var sx = s[i]; if(sx != 1){ s[i] = sx-1; break; } if(i == n) return [sum, maxflips]; // Out of permutations. s[i] = i; // Rotate 1<-...<-i+1. var t = p[1]; for(var j = 1; j <= i; j++){ p[j] = p[j+1]; } p[i+1] = t; } } } } function getTimeSec(){ var d = new Date(); return (d.getTime() + d.getMilliseconds() / 1000.0) / 1000.0; } var n = 10; var start_time = getTimeSec(); var r = fannkuch(n); var sum = r[0], flips = r[1]; WScript.Echo( sum,"\n", "Pfannkuchen(",n,") = ",flips,"\n", "time = ",(getTimeSec() - start_time),"\n" ) 

After that, it is enough to run this script twice (first in Node, then in WSH) - and we will get this result in the console (and in the screenshot ):

[screenshot]

The difference in two orders! The calculations that Node.js copes with in a second, Windows Script Host grinds for more than two minutes.

I made the measurement in Windows XP SP3 on a Pentium 4 processor (2.2 GHz). Each of you can independently repeat it in your own using the method described above.

The conclusion is also quite simple and accessible to everyone: with the advent of the V8 engine , the JavaScript language can be said to have experienced a rebirth. Its computational capabilities have increased a hundredfold, although the rules of the language have not changed one iota (short of the appearance of some optional new products from the ECMAScript Harmony draft copy).

In the real world, this happens to people only in superhero comics. And in programming, it happens easily and real: the developer programmed in javascript, then V8 appeared - women, and all the scripts began to work a hundred times faster.

And this should give a reason for further deepest reflection on the prospects. Here, for example: evgeniyup yesterday measured that PHP 5.3.3 doesn’t work much faster than WSH. Somewhere in 1.2 times. So, there must be some potential in this area for a sharp increase in speed using JIT compilation and other V8-like tricks, whereas so far all the so-called PHP accelerators have been doing, by best, byte-code caching and trying to optimize it. , but not more. Imagine how the WWW world around us will change if PHP starts to execute a hundred times faster.

Or at least an order of magnitude faster.

Source: https://habr.com/ru/post/153697/


All Articles