FP 在 V8 中使代码 运行 更快吗?

does FP make code run faster in V8?

最近和Javascript(Chrome)玩了很多,有一些想起来的事情。

  1. V8 有一个 JIT,可以使代码 运行ning 更快。
  2. 函数式编程意味着你将逻辑写入函数并invoke/combine它们通过链式调用,意味着核心函数将被频繁调用(不是它的真正定义只是为了说明我的观点)。
  3. JIT 是 exchange time with space 的一个最佳实践,主要是在第一次缓存高级函数的机器代码和 运行 下次缓存。

所以我可以说,如果以 FP 方式和 运行 具有 JIT 功能的 VM 编写代码,应用程序会更快。

这里是关于这个主题的好读物:http://thibaultlaurens.github.io/javascript/2013/04/29/how-the-v8-engine-works/

特别是 V8 如何编译和注入 JIT 代码的部分

How V8 compiles JavaScript code?

V8 has two compilers!

A “Full” Compiler that can generate good code for any JavaScript: good but not great JIT code. The goal of this compiler is to generates code quickly. To achieve its goal, it doesn’t do any type analysis and doesn”t know anything about types. Instead, it uses an Inline Caches or “IC” strategy to refine knowledge about types while the program run. IC is very efficient and bring about 20 times speed improvement.

An Optimizing Compiler that produces great code for most of the JavaScript language. It comes later and re-compiles hot functions. The optimizing compiler takes types from the Inline Cache and make decisions about how to optimize the code better. However, some language features are not supported yet like try/catch block for instance. (The workaround for try/catch blocks is to write the “non stable” code into a function and to call the function in the try block)

简而言之,最快的代码是那些在定义对象或原型函数定义后不修改的代码