从v8中挤出性能 [英] Squeezing performance out of v8

查看:94
本文介绍了从v8中挤出性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有关于如何为v8编写快速,高效的代码(特别是对于node.js)的好教程?

Are there any good tutorials on how to write fast, efficient code for v8 (specifically, for node.js)?

我应该避免使用哪些结构? v8优化得很好的成语是什么?

What structures should I avoid using? What are the idioms that v8 optimises well?

推荐答案

根据我的经验:


  • 内嵌

  • 函数调用开销最小(内联)

  • 价格昂贵的是通过巨大的字符串到函数,因为那些需要复制,根据我的经验,V8并不总是像在这种情况下那样聪明

  • 范围查找很昂贵(惊喜)

  • 不要做伎俩,例如我有一个JS Object的二进制编码器,通过位移(而不是Math.floor)最新的 Crankshaft (是alpha,但仍然)运行代码慢30%

  • 不要使用魔法。 eval,arguments.callee等。由于代码无法再内联,因此几乎无法进行任何优化

  • 一些新的ES5内容,例如: .bind()目前在V8中真的

  • 不知怎的新的Object()新的Array()目前有点快(MICRO优化,除非你用<$ c写一些疯狂的编码器棒) $ c> {} 和 []

  • It does inlining
  • Function call overhead is minimal (inlining)
  • What is expensive is to pass huge strings to functions, since those need to be copied and from my experience V8 isn't always as smart as it could be in this case
  • Scope lookup is expensive (surprise)
  • Don't do tricks e.g. I have a binary encoder for JS Object, cranking out some extra performance with bit shifting there (instead of Math.floor) latest Crankshaft (yes alpha, but still) runs the code 30% slower
  • Don't use magic. eval, arguments.callee etc. Those pretty much kill any optimization since code can no longer be inlined
  • Some of the new ES5 stuff e.g. .bind() is really slow in V8 at the moment
  • Somehow new Object() and new Array() are a bit faster currently (MICROoptimization, unless you're writing some crazy encoder stick with {} and [])

我的规则:


  • 写好代码

  • 编写工作代码

  • 编写在严格模式下工作的代码(支持仍然有登陆,但当它是否可以通过V8进行进一步优化

  • Write good code
  • Write working code
  • Write code that works in strict mode (support still has to land, but when it does further optimization can be applied by V8)

如果您是JS专家并且您已经应用了所有良好做法代码,您几乎无法采取任何措施来提高性能。

If you're an JS expert and your already applying all good practices to your code, there's hardly anything you can do to improve performance.

如果遇到性能问题:


  • 验证它们

  • 更改代码/算法

  • 作为最后的手段:写一个C ++扩展(并观察每个提交到GitHub上的ry / node,因为没有人关心是否有一些内部更改破坏了你的构建)

  • Verify them
  • Change the code / algorithm
  • And as a last resort: Write a C++ extension (and watch every commit to ry/node on GitHub since nobody cares whether some internal changes break your build)

这篇关于从v8中挤出性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆