In javascript, access to "window.Math" is slower or faster than accessing a "math" object without a "window"?

I'm curious what is best when referencing the "global" namespace in javascript, which is just a shortcut to the object window(or vice versa) depending on how you look at it).

I want to know if:

var answer = Math.floor(value);

better or worse:

var answer = window.Math.floor(value);

Is it better or worse, even a little, for performance, resource use, or compatibility?

Do you have a lower cost? (Something like an extra pointer or something else)

Edit note . Although in most cases I read Nazi performance data, in this case I ignore differences in readability in order to focus solely on productivity.

+5
source share
6 answers

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 ).
+14

JS .

: . for, , ... , . !

+2

, , Scope Chain the Identifier Resolution.

- , , , ().

, .

with catch, .

:

// global code
var var1 = 1, var2 = 2;
(function () { // one
  var var3 = 3;
  (function () { // two
    var var4 = 4;

    with ({var5: 5}) { // three
      alert(var1);
    }
  })();
})();

, , , with, var1 var2, 4 , , : , with, , , .

, window - , , . window , .

, window, ( - ), , , window.Math , dot (.).

+2

( ) Math.floor, , window.Math ( window Javascript) Javascript, V8.

Spidermonkey V8 , .

Math.floor, , . 100 000 , , , .

v8, , this int.Parse() .

// Some people use parseInt instead of Math.floor.  This
// optimization makes parseInt on a Smi 12 times faster (60ns
// vs 800ns).  The following optimization makes parseInt on a
// non-Smi number 9 times faster (230ns vs 2070ns).  Together
// they make parseInt on a string 1.4% slower (274ns vs 270ns).
+1

JavaScript-, , something, . window . , window.Math, , window, Math . Math, , , .

, - Math.something , window.Math.something.

. http://video.yahoo.com/watch/111593/1710507, , .

+1

If Math.round()called in the local / functional area, the interpreter will have to check the local var first, and then in the global / window space. Therefore, on a local scale, I assumed that it window.Math.round()would be very slightly faster. This is not an assembly or C or C ++, so I would not worry about which is faster for performance reasons, but if out of curiosity, of course, compare it.

0
source

All Articles