这是什么风格ASM英寸×| 0 QUOT;现在一些JavaScript程序员使用? [英] What is this asm style "x | 0" some javascript programmers are now using?

查看:174
本文介绍了这是什么风格ASM英寸×| 0 QUOT;现在一些JavaScript程序员使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些性能至关重要的javascript code,就像一个在这个项目的大量使用位或操作以0例如:

I've seen some performance critical javascript code, like the one on this project that makes extensive use of bitwise OR operations with 0. Ex:

GameBoyAdvanceCPU.prototype.write8 = function (address, data) {
address = address | 0;
data = data | 0;
this.memory.memoryWrite8(address | 0, data | 0);

我知道用例地板号码用| 0,但在这里,情况并非如此,因为这些是总是INT的。它看起来有点像asm.js,这是告诉我们正在与整数工作,使一些优化JS引擎?如果是这样,这浏览器会这些优化?

I know about the use case of flooring numbers with "|0", but that isn't the case here, as these are always int's. It looks a bit like asm.js, is this to tell the js engine that we are working with integers, allowing some optimizations? If so, which browsers will make those optimizations?

任何指针如何工作将是AP pretiated。

Any pointers to how this works would be appretiated.

推荐答案

据的 JavaScript性能的狂人

结束语整数运算前pressions()| 0 允许运行,以确保你正在做的整数运算代替浮点运算。这使得它避免检查溢出,并在许多情况下产生更快的code。

Wrapping integer arithmetic expressions in ( ) | 0 allows the runtime to be sure that you're doing integer arithmetic instead of floating-point arithmetic. This allows it to avoid checking for overflow and produce faster code in many cases.

和根据页面,它是最的Javascript运行时真实的,但并没有说哪个。

and according to the page, it's true for "most" Javascript runtimes, but doesn't say which.

作为第二个来源,<一个href=\"http://www.gamedev.net/page/resources/_/technical/game-programming/writing-fast-javascript-for-games-interactive-applications-r3516\"相对=nofollow>编写快速的JavaScript对于游戏和放大器;交互式应用状态

要告诉JavaScript引擎,我们要存储整数值[...]我们可以使用按位或操作:

To tell JavaScript engine we want to store integer values [...] we could use bitwise or operator:

和来自微软编写高效的JavaScript页​​面

[...]明确地告诉JavaScript的运行时使用整数运算[...]使用按位或操作

[...] explicitly tell the JavaScript runtime to use integer arithmetic [...] use the bitwise or operator

此外,除了在注释中,没有提到以上的asm.js网页,所以我怀疑这样的优化在code申请没有明确标记为ASM /在不明确承认它的浏览器。

Also, apart from in comments, none of the pages above mention asm.js, so I suspect such optimizations apply in code not explicitly marked as asm/in browsers that don't explicitly recognize it.

这篇关于这是什么风格ASM英寸×| 0 QUOT;现在一些JavaScript程序员使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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