문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top