performance - In javascript, is accessing 'window.Math' slower or faster than accessing the 'Math' object without the 'window.'? -


I do the best practice while referring to the 'global' namespace in javascript, which is only one window Shortcut to an object or (or vice versa, depending on how you see it).

I want to know that:

  var answer = monastery. Floor (value);  

This is better or worse than:

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

Is there a little better or worse for performance, processing, or compatibility?

Is anyone's cost less? (An additional indicator or something like this)

Edit note : While I am a readability on Nazi performance in most situations, in this case I will notice differences in readability I'm ignoring completely on performance.

First of all, never compare things like these because of performance. Math.round is clearly easier on the eyes than window.Math.round , and you will not see a significant performance increase using one or the other, then some Do not impersonate your code to increase performance.

However, if you are only curious what you can think about ... I am not sure how to look at the globally "hood", but I think the window To reach , math (like reaching the window and Math ) stay on the same level, as Window.window.window.Math.round is working). Thus, it will be slow to reach window.Math .

In addition, the way the variable is viewed, you will increase the performance by var round = Math.round; and calling rounds (1.23) , since all names are first seen in the local area, then the existing multiple radius, and so on, global workspaces in all respects. Every level of scope connects very little fluctuation.

But again, do not optimize this unless you're not sure they will make a noticeable difference

Here's a complete framework using Firebug:

  & lt ;! DOCTYPE html & gt; ; & Lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; Benchmark Scope Lookup & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Script & gt; Function bench_window_Math_round () {for (var i = 0; i & lt; 100000; i ++) {window.Math.round (1.23); }} Function bench_Math_round () {for (var i = 0; i & lt; 100000; i ++) {Math.round (1.23); }} Ceremony bench_round () {for (var i = 0, round = Math.round; i & lt; 100000; i ++) {round (1.23); }} Console.log ('Profiling will start in 3 seconds ...'); {Var i = 0; i & lt; 10; i ++} {bench_window_Math_round (); bench_Math_round (); bench_round ();} console.profileEnd (); SetTimeout (function () for {console.profile ()} }, 3000); & Lt; / Script & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

My results:
Time 100,000 * shows total for 10 calls, average / minimum / maximum 100,000 calls

  Call Percentage Time Time Average Minimum Max Bench_window_meth_10 10 86.36% 1114.73ms 1114.73ms 111.473ms 110.827ms 114.018ms BenchMath_Audio 10 8.21% 106.04ms 106.04 MS 10.604 MS 10.252 MS 13.446 MBP Banquet 10 5.43 % 70.08 MS 70.08 MS 7.008 MS 6.884 MS 7.0 9 2M  

As you can see, window.Math is a very bad idea I think Accessing the global window object adds an additional overhead, however, with a global scope There is a difference in reaching the math object, and it is not very good to reach the local variable with a reference to the Math.round function. Keep in mind that this is 100,000 calls , And the difference is only 3.6ms. Even with one million calls you see only 36ms difference.

Things to think about the above profile code:

  • The function is actually seen from any other radius, which adds an overhead (though hardly notable, I tried to import tasks into an anonymous function)
  • Adds the actual Math.round function overhead (I'm estimating approximate 6ms in 100,000 calls).

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -