First of all, never compare such things for performance reasons. Math.roundobviously easier in the eyes than window.Math.round, and you won't see a noticeable increase in productivity using one or the other. Therefore, do not reduce your code for a very slight increase in performance.
, , ... , " ", , window Math (window Math , window.window.window.Math.round). , window.Math .
, , , var round = Math.round; round(1.23), , , .., . .
, , , . , , , , .
Firebug:
<!DOCTYPE html>
<html>
<head>
<title>Benchmark scope lookup</title>
</head>
<body>
<script>
function bench_window_Math_round() {
for (var i = 0; i < 100000; i++) {
window.Math.round(1.23);
}
}
function bench_Math_round() {
for (var i = 0; i < 100000; i++) {
Math.round(1.23);
}
}
function bench_round() {
for (var i = 0, round = Math.round; i < 100000; i++) {
round(1.23);
}
}
console.log('Profiling will begin in 3 seconds...');
setTimeout(function () {
console.profile();
for (var i = 0; i < 10; i++) {
bench_window_Math_round();
bench_Math_round();
bench_round();
}
console.profileEnd();
}, 3000);
</script>
</body>
</html>
:
Time 100 000 * 10, Avg/Min/Max 100 000 .
Calls Percent Own Time Time Avg Min Max
bench_window_Math_round
10 86.36% 1114.73ms 1114.73ms 111.473ms 110.827ms 114.018ms
bench_Math_round
10 8.21% 106.04ms 106.04ms 10.604ms 10.252ms 13.446ms
bench_round
10 5.43% 70.08ms 70.08ms 7.008ms 6.884ms 7.092ms
, window.Math - . , window . Math Math.round ... , 100 000 , 3,6 . 36 .
:
- , ( , ).
Math.round ( 6 100 000 ).