Do local variables take 7 times longer than global variables?

I tried to measure the gain / loss of "caching" math.floor, in the hope that I could make faster calls.

Here is the test:

<html> <head> <script> window.onload = function() { var startTime = new Date().getTime(); var k = 0; for(var i = 0; i < 1000000; i++) k += Math.floor(9.99); var mathFloorTime = new Date().getTime() - startTime; startTime = new Date().getTime(); window.mfloor = Math.floor; k = 0; for(var i = 0; i < 1000000; i++) k += window.mfloor(9.99); var globalFloorTime = new Date().getTime() - startTime; startTime = new Date().getTime(); var mfloor = Math.floor; k = 0; for(var i = 0; i < 1000000; i++) k += mfloor(9.99); var localFloorTime = new Date().getTime() - startTime; document.getElementById("MathResult").innerHTML = mathFloorTime; document.getElementById("globalResult").innerHTML = globalFloorTime; document.getElementById("localResult").innerHTML = localFloorTime; }; </script> </head> <body> Math.floor: <span id="MathResult"></span>ms <br /> var mathfloor: <span id="globalResult"></span>ms <br /> window.mathfloor: <span id="localResult"></span>ms <br /> </body> </html> 

My test results:

 [Chromium 5.0.308.0]: Math.floor: 49ms var mathfloor: 271ms window.mathfloor: 40ms [IE 8.0.6001.18702] Math.floor: 703ms var mathfloor: 9890ms [LOL!] window.mathfloor: 375ms [Firefox [Minefield] 3.7a4pre] Math.floor: 42ms var mathfloor: 2257ms window.mathfloor: 60ms [Safari 4.0.4[531.21.10] ] Math.floor: 92ms var mathfloor: 289ms window.mathfloor: 90ms [Opera 10.10 build 1893] Math.floor: 500ms var mathfloor: 843ms window.mathfloor: 360ms [Konqueror 4.3.90 [KDE 4.3.90 [KDE 4.4 RC1]]] Math.floor: 453ms var mathfloor: 563ms window.mathfloor: 312ms 

Dispersion is random, of course, but for the most part

In all cases [this shows the time]:
[takes longer] mathfloor> Math.floor> window.mathfloor [faster]

Why is this? In my projects, I used var mfloor = Math.floor , and according to my not-so-impressive criteria, my attempts to "optimize" actually slowed down the script with ALOT ...

Is there any other way to make my code more "efficient" ...? I am at a stage where I basically need to optimize, so no, this is not "premature optimization" ...

+6
optimization javascript benchmarking
source share
3 answers

You have these two variables labeled incorrectly:

 var mathfloor: <span id="globalResult"></span>ms <br /> window.mathfloor: <span id="localResult"></span>ms <br /> 

@ David alternatives are noteworthy, as are some types of memoisation.

+2
source share

I'm not sure why your tests do what they do.

But if you are going to often call Math.floor, you can use this:

 var num = 9.99; var floored = ~~num; // 9 

Not that ~~ will probably not work on strings ( var num = "9.99" ), the numbers are in the 32-bit range, but negative numbers ( ~~ are rounded).

See this question for more information.


UPDATE
Here is the modified criterion

In Chrome, I get a local scope returning faster than Math.floor and the global scope. (window.mfloor) Note that I am not referring to the global mfloor with the window. syntax window. as in the original test.

So, I think your test has 2 problems (besides mixing the variable name mentioned in other answers). One of them was that you started the loop on window.mfloor, and the other - you had a local variable with the same name as the global variable (this is just an assumption).

Try the test using the jsbin link that I posted and get back to me.


here is my guideline for the lazy:

 window.onload = function(){ var k = 0, i=0, n = 2000000, startTime = +(new Date); for(; i < n; ++i) k += Math.floor(9.99); var mathFloorTime = (new Date) - startTime; window.globalMfloor = Math.floor; k = i = 0; startTime = +(new Date); for(; i < n; ++i) k += globalMfloor(9.99); var globalFloorTime = (new Date) - startTime; var mfloor = Math.floor; k = i = 0; startTime = +(new Date); for(; i < n; ++i) k += mfloor(9.99); var localFloorTime = (new Date) - startTime; alert("Math.floor: " + mathFloorTime); alert("globalMfloor: " + globalFloorTime); alert("mfloor: " + localFloorTime); };​ 
0
source share

Edit:. I did not read the answer about mixing the field name. It turns out that in Firefox access to the local variable was faster (occupying 80% in length) as you accessed Math.floor, but access to the global variable took 140%.

In my original answer, I postulated that local variables are harder to access than global variables due to closure handling and something else. However, it seems the opposite.

0
source share

All Articles