2

Will this script

var a = 1/3;
var b = 2/5;
var c = a+b;

run faster or slower than this script

var a = Math.round(100*(1/3))/100;
var b = Math.round(100*(2/5))/100;
var c = a+b;

Or rather, is there a way to get javascript to evaluate an equation to only a certain level of accuracy.

var a = Math.onlySolveThisUpTo2DecimalPlaces(1/3); //0.33

Is the speed difference even large enough to care about?

user2224693
  • 122
  • 7
  • premature optimization is the root of all evil... – dandavis Feb 13 '14 at 21:25
  • I've had a question about `Math.round()` effeciency also. Consider bitshifting if speed is of upmost importance. See here: http://stackoverflow.com/questions/3233731/javascript-bitshift-alternative-to-math-round – cube Feb 13 '14 at 22:21

1 Answers1

3

Will this script […] run faster or slower than this script

Faster. There’s much more to do in the second.

Or rather, is there a way to get javascript to evaluate an equation to only a certain level of accuracy.

No.

Is the speed difference even large enough to care about?

No.

If you want to display the result with a certain precision, you can use a.toFixed(2) (two decimal places) or a.toPrecision(2) (two significant digits). But rounding (and not even to integers) isn’t going to positively affect performance.

Ry-
  • 218,210
  • 55
  • 464
  • 476