Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top