English 中文(简体)
Javascript Math.ceil(Math.abs())优化
原标题:Javascript Math.ceil(Math.abs()) optimization

I m using Math.ceil ( Math.abs (x ) within a loop.

Can anyone realize any optimization for this operation? (Bitwise or what?)

欢迎你在上的基准。

最佳回答
问题回答

<代码>x” 0 ? Math.ceil(-x) :Math.ceil(x)

http://jsperf.com/math-ceil-math-abs-x/6”rel=“nofollow”jsPerf page 。 Ignore the "some bitwisemente”bell;以上表述并不使用任何标签。

Javascript isn t a prepared Language such C, so bitwise operations that can work questions in such instruments, aren t so high in JS, as number are Deposit as 64 bitroting Point. 查阅 SO post

即便如此,你在联合材料中撰写的内容将在一定程度上通过原始浏览器转化为本地的法典,而根据执行情况,这可能会更快或更慢。

Math.ceilMath.abs建立以来;我猜测它们重新优化,因此,我怀疑你能通过自己做一些trick,获得更好的业绩。

底线:三个东西阻碍你更快地做到这一点:

  1. number representation in JS
  2. fact that it s an interpreted language
  3. functions you use are "native", so they should be fast enough on their own

parseInt(Math.abs(x)+ 1,在代谢时,其速度为30%左右

由于这一论点总是正面的,Math.ceil()的分支是不必要的。

这里,不需要做任何研究:

((x >= 0 ? x : -x) + 0.5) >> 0

两种最快的计算方法(现代浏览器的速率几乎相同):

function f (n) {
   return (~~n) + 1;
}

// or 

function f1 (n) {
   return (n | 0) + 1;
}

// some tests, ~~ operator seems to work identicaly on numbers:

( 3.3 | 0 ) === 3;   
( 3.8 | 0 ) === 3;   
( -3.3 | 0 ) === -3; 
( -3.8 | 0 ) === -3;  

与<代码>Math.底(-3.3)=Math(-3.8)== -4





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签