Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 809
Jeff Avatar asked Nov 18 '10 22:11

Jeff


2 Answers

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.

like image 160
fedesilva Avatar answered Oct 21 '22 21:10

fedesilva


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?

like image 32
2 revs Avatar answered Oct 21 '22 22:10

2 revs