V8 engine compiles JavaScript to machine code. So, why node.js isn't faster than C?
Asked Answered
F

5

6

According to language benchmarks, JavaScript V8 is faster than other programming languages at regex-dna program. So, why node.js applications (i.e. http server) isn't faster than C applications (i.e. Nginx, Lighttpd)?

Feckless answered 18/11, 2010 at 22:36 Comment(9)
Why is apache faster than lighttpd, they are both programmed in C?Ricks
Now that is a different question @RicksAppetizer
You do know that C is also compiled to machine code, don't you?Chirography
The Javascript V8 engine itself is written in C++ (not that that would necessarily matter)Schwartz
According to the figures a Ferrari is faster than many other vehicles at going round a racetrack, so why doesn't it go faster than a tractor when pulling a plough?Jonme
@Jonme awesome analogy, although it would be sad to run a Ferrari through fields :(Schwartz
@Sagar: I saw more or less exactly that when I was in Ireland over the summer. Still not sure where the farmer got the Ferrari, though.Undesirable
@NickFitz: So, using regex in C is racing with tractor and building an HTTP server in Node.js is pulling a plough with a Ferrari?Feckless
@PolatAlemdar ~ Please understand that the V8 and other runtimes for interpreted languages are often optimized for using Regexes because that is one of the primary use cases of those languages. The primary use-case of a webserver is NOT to process Regexes. So while the V8 engine may be optimized for Regex, that has no bearing on it's ability to handle IO sockets or IO file requests.Appetizer
F
34

Because V8 applications are javascript applications. Even if the javascript is finally compiled to machine code the runtime characteristics are different.

For example if you call a function in an object and that object does not define the function the runtime must locate the function by traversing the prototype hierarchy, this hierarchy can change at any time during the lifetime of a program. There are clever optimizations that can be done but the overhead exists nevertheless.

There is also the memory model. Javascript is garbage collected and GC takes cpu cycles.

Flawed answered 18/11, 2010 at 23:3 Comment(0)
U
11

Because serving http requests is a different problem than regex-dna.

The fact that A is faster than B at one task doesn't say anything about what to expect from some other task.

Obligatory bad car analogy: A ham sandwich is much tastier than a porsche. Why isn't it faster from 0-60?

Undesirable answered 18/11, 2010 at 22:36 Comment(3)
Parsing HTTP requests and regex mustn't be irrelevant as in your semantic example. Anyways, Would you drive a sandwich or eat a porsche to code an HTTP server?Feckless
@PolatAlemdar ~ There is a language barrier here. Those are idioms in English.Appetizer
-1 for the bad car analogy, the benchmarks game website Polat Alemdar points to itself shows another half-dozen different tasks, and shootout.alioth.debian.org/flawed-benchmarks.php#thinkingOstracod
W
3

For one C is essentially the benchmark language for computation speed. The compliers these days are very good and produce very efficient machine code. What this means is that the upper limit for performance is what you can get with C.

In one of his presentations about node.js Ryan Dahl (the creator) said that node is about as fast as C. (but GC and other things do have an impact on performance, not to mention that V8 needs time to properly optimize things)

Wixted answered 19/11, 2010 at 6:18 Comment(0)
N
0

In the that benchmark there are 15 different programs and only one dna-regex benchmark where you can see JavaScript V8 is faster. On other benchmarks JavaScript is much lower in performance. You can choose program in the first select, look at this one.

Noma answered 17/3, 2014 at 12:57 Comment(0)
O
-5

According to language benchmarks, JavaScript V8 is not faster than GCC at binary-trees program.

According to language benchmarks, JavaScript V8 is not faster than GCC at k-nucleotide program.

According to language benchmarks, JavaScript V8 is not faster than GCC at n-body program.

According to language benchmarks, JavaScript V8 is not faster than GCC at spectral-norm program.

According to language benchmarks, JavaScript V8 is not faster than GCC at fasta program.

According to language benchmarks, JavaScript V8 is not faster than GCC at reverse-complement program.

According to language benchmarks, JavaScript V8 is not faster than GCC at pidigits program.

Ostracod answered 19/11, 2010 at 17:18 Comment(1)
@Polat Alemdar - You asked "So, why node.js applications (i.e. http server) isn't faster than C applications" and the answer is that you have no reason to think they should be faster than C applications. You took one measurement out of context and ignored the other 7.Ostracod

© 2022 - 2024 — McMap. All rights reserved.