Why is Math.round in javascript slower than a custom built function?
<p>I was messing around with creating a custom rounding function that could round to whatever interval I wanted. eg(if I was working with degrees it would round to the nearest 15 degrees)  Anyways I decided to see how fast it was compared to Math.round and come to find out it is slower.  I am using firebug on FF8</p>

<pre><code>function R1(a,b){var c=a%b;return a-c+(c/b+1.5&gt;&gt;1)*b}

function R2(a){return Math.round(a)}

var i,e=1e5;
console.time('1');
i=e;
while(i--){
  R1(3.5,1);
}
console.timeEnd('1');
console.time('2');
i=e;
while(i--){
  R2(3.5);
}
console.timeEnd('2');
</code></pre>

<p>and my results were</p>

<pre><code>1: 464ms
2: 611ms
</code></pre>

<p>I ran them several times in different ways, but always R1 came out faster.  Maybe this is just a FF thing, but if so what is causing it.</p>

<p><strong>EDIT:</strong>
I took each out of the function call to see what would happen</p>

<pre><code>var i,e=1e5,c;
console.time('1');
i=e;
while(i--){
  c=3.5%1;
  3.5-c+(c/1+1.5&gt;&gt;1)*1;
}
console.timeEnd('1');
console.time('2');
i=e;
while(i--){
  Math.round(3.5);
}
console.timeEnd('2');
</code></pre>

<p>and the times I'm getting</p>

<pre><code>1: 654ms
2: 349ms
</code></pre>
