I’ve seen a number of questions about simulations and animations in javascript, which often involve calculating the hypotenuse:

```
hypot = Math.sqrt(x*x + y*y);
```

Since cartesian coordinates are the weapon of choice in most of these engines, these calculations are needed to find the distance between pairs of points, etc. So any speedup in calculating the hypotenuse could be a great help to many projects.

To that end, can you see a faster method than the simple implementation above? I found an approximation which was marginally faster in Chrome, but turned out to be much slower in Firefox, based on this approximation function in SuperCollider.

Edit 2015-08-15: I’ve switched the accepted answer to being the Math.hypot one; I suspect the pragmatic approach at present would be to use Math.hypot or a synthesized hypot function if not available, and to compare against the square (per sch’s answer) if that is sufficient and Math.hypot is not available.

An important point that many do not know:

Reference and complete text: John D. Cook – http://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/

You can look to equality of

`x`

and`y`

. If are equals you can calculate hypotenuse as`(x + y)/sqrt(2)`

where`sqrt(2)`

is a constant.So this method can be used for case where x = y. For other cases it can be used with maximum imprecision of ~41%. This is a big error. But when you specify allowable error limits you can use this method. For example, if define allowable error to 5% you can get that

`b`

must be between`0.515*a`

and`1.942*a`

.So if you don’t need perfect imprecision of your calculations, you can improve performance of calculations with range of values.

By analogy you can look to equality of

`x`

or`y`

to`zero`

. And with some accuracy calculate hypotenuse more faster for this cases.P.S. I’ve read about this in the one russian article.

Often, you don’t need to compute the square root and

`hypot^2 = x*x + y*y`

is enough. This is the case for example if you want to compare the distances and don’t need the actual values.Performance of

`Math.sqrt()`

and`Math.hypot()`

depends on javascript running environment.I just run this code in Node.js, Chrome and Firefox:

with the following results:

Node.js v14.15.4:

Chrome 88.0.4324.150:

Firefox 85.0.2:

which means V8 has awful implementation of Math.hypot(). Actually I wouldn’t be much surprised if it also depended on CPU architecture/model.

Note that in my example I feed integer numbers to

`Math.sqrt()`

and`Math.hypot()`

. Another test revealed that with floating-point numbers Node.js runs test code 20% slower than on integers. In Chrome`Math.sqrt()`

performance is exactly the same and`Math.hypot()`

runs like 3% slower. Firefox: no difference.In ECMAScript ES6 you can use

Math.hypot:Edit: You can run this test on a blank tab, are 2 million operations with both methods, the results are very good, it is 24% faster.Note: In this test, it’s used ES6’sMath.hypot.Wkovpk – http://virviaga.com/ sildenafil coupon

Aptdiu – http://virviaga.com/ viagra generic

Obvkas – ivermectin 12 mg Psfkzw dizxoe

Gorwzh – cialis and viagra together Deggcx fmxfwt

Nyvecc – furosempi.com Hhamuk mqspaq

Ogtlnn – kamapls.com Minbhl jdfrpr

Zxexdc – canada buy cialis online Sxtnol iumglk

Paxfkf – how much is the generic viagra Qfmwmf avhdhz

Pwxcyy – academic essay writing knowledge deficit Luhuub vzgdmq

Joatac – purchase viagra with paypal Vwqogl jpozbi

Pvyvxf – free lance writing Vzqaye cblpnp

Pduqts – brand name propecia online Kjlwbk grixxk

Klbfru – viagra original ou generique Kcxjoa aakztu

Cgcbsz – amazon finasteride 1mg Cvsvwf xmszfr

Irthcg – process of essay writing Sqokjt mlnyuq

Dkidto – how to pronounce sildenafil Lkdvej geulfm

Ahnoin – college application essay writing service Xvsyip pvscnb

Iecjpy – lasix 40mg Bkrysa iimdez

Zgihyl – levitra 20mg Gcwuib gooenp

Bueqgr – buy silagra 100 Xwlkuj tiqcks

Cwflgb – tadacip 4575 Ozxsit njcdhb

Twrans – best price for extra super avana on the internet Dxichl glzqim

tadacip 20 online – tadacip 3219 order tadacip

celebrex uk – price of celebrex 200mg capsules celecoxib 200 mg no prescription

order stromectol – stromectol 3 mg stromectol for head lice

yasmin australia price – nexium 5mg coupon buy yasmin usa

prednisone 2.5 – prednisone 2.5 mg cost prednisone 30

purchase viagra tablets – viagra professional 100mg canada viagra online india pharmacy

viagra online paypal canada – viagra sildenafil female viagra for sale uk

lyrica tablet cost – lyrica 10 mg lyrica pills price

where to buy nexium in singapore – nexium tablets canada yasmin 3mg price

levitra cost comparison – https://levitrahims.com/ viagra levitra and cialis

http://costpills.com – sildenafil tablet brand name Ajzp54g

viagra from canada http://prescriptionhim.com/ – buying viagra online

kamagra – http://www.kamagraxy.com/#

ivermectin ebay – ivermectin 3mg ivermectin cream cost

cost of stromectol – ivermectin 50 mg stromectol coronavirus

ondansetron 1mg – zofran medicine over the counter 4mg ondansetron

ivermectin rezeptfrei https://stromectolhumans.com/# como tomar ivermectina

purchase viagra no prescription https://viadrprescription.com/# viagra without a prescription

viagra pfizer – http://www.doctorxep.com/