performance - Counter Intuitive javascript sqrt performace? -


i'm taking webdev class , 1 of tasks had calculate if age perfect square. seeing of different solutions students came with, curious actual performance wrote little test see how stack each other, , turns out 1 calls math. functions far , away fastest consistently.

here functions:

var func1 = function(age){   var agesq = math.floor(math.sqrt(age)) * math.floor(math.sqrt(age));   return agesq === age; } var func2 = function(age){   return math.sqrt(age) % 1 === 0; } var func3 = function(age){   return number.isinteger(math.sqrt(age)); } var func4 = function(age){   return (parseint(math.sqrt(age)) * parseint(math.sqrt(age)) == age) } 

running each of these on 100,000 array of integers 0-99 total time each follows 3 runs:

[4.405000000000015, 8.625000000000014, 10.54000000000002, 7.895000000000039] [3.989999999999807, 6.310000000000002, 6.780000000000001, 6.444999999999993] [3.584999999999795, 13.98500000000014, 11.47999999999999, 9.965000000000003] 

as can see first 1 dramatically faster - looking @ code have guessed slowest. idea what's going on here under hood?


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -