首页 文章

使用Chrome和Node.js时,是否在客户端和服务器上编译了javascript? [关闭]

提问于
浏览
2

我总是将脚本语言(javascript,vscript,任何类型的“脚本”)视为解释,因此比C和C等编译语言慢 .

"JavaScript is an interpreted language, with optional JIT-compilation support. In older implementations (e.g. Internet Exlorer 8 and earlier, Firefox prior to 3.5), JavaScript was a purely interpreted language. This means that scripts execute without preliminary compilation, i.e. without conversion of the script text into system-dependent machine code." - http://www.javascripter.net/faq/whatisja.htm

在阅读以下内容时,显然谷歌浏览器(使用V8)在客户端上编译javascript:

"Here’s how it works. First, V8 defers compilation of JavaScript functions until immediately before they are executed the first time (to reduce the overall time spent compiling). Next, pieces of code that are executed very often are compiled a second time by a specialized optimizing compiler. This second pass makes takes more time, but thanks to many advanced optimization techniques, it delivers much faster code." - http://thenextweb.com/google/2014/02/13/google-speeds-chrome-compiling-javascript-background/

这听起来像在V8上,javascript现在是一种编译语言,因为它们在看到函数后立即将每个函数编译成机器代码,然后才执行 .

此外,这个https://gist.github.com/spion/3049314似乎也表明(对于这个测试用例)V8上的javascript甚至比编译C做同样的事情要快 .

这是否意味着javascript实际上在客户端和服务器上执行之前被编译成机器代码(因为node.js使用V8)因此运行为编译的机器代码(如java,C或C)而不是解释代码(如Perl,PHP或Powershell)具有相关的执行速度优势?

口译语言:

"The main disadvantage of interpreting is a much slower speed of program execution compared to direct machine code execution on the host CPU" - http://en.wikipedia.org/wiki/Interpreted_language

编译语言:

"Compiled languages are always supposed to be fast because of their direct execution by the computer." - http://www.codeproject.com/Articles/696764/Differences-between-compiled-and-Interpreted-Langu

2 回答

  • 3

    是的,在2014年,每个人都使用各种策略将JavaScript编译为本机代码以优化代码 . 甚至有像asm.js这样的标准允许以这样的方式编译JavaScript,使得生成的代码至少与用C / C或Java编写的代码一样快 .

    与往常一样,您最终获得的性能实际上取决于优化和您的代码 . 正如在Java或C中编写慢速代码一样容易,您也可以编写一些世界上没有自动优化器可以修复的东西 .

  • 1

    是的,它是编译的,但编译本身并没有做任何快速的事情,这是编译器的优化 .

    事实上,V8完全没有与其他引擎不同的解释器,这是一种设计权衡 - 它并不意味着解释比未经优化的编译代码慢 .

相关问题