Why is Javascript's Math.floor the slowest way to calculate floor in Javascript?

前端 未结 2 993
醉话见心
醉话见心 2020-12-05 05:00

I\'m generally not a fan of microbenchmarks. But this one has a very interesting result.
http://ernestdelgado.com/archive/benchmark-on-the-floor/

It suggests t

2条回答
  •  离开以前
    2020-12-05 05:25

    It has nothing to do with modern browsers. It has to do with implementing the ECMA standard. You can't just change how a certain function performs even if there is a faster way. It could break existing code.

    The Math.Floor has to account for a lot of different scenarios of handling different types. Could they have made different scenarios faster by taking short cuts as you described? Maybe they could, but that might have broken other scenarios. Just because something on the surface looks small, doesn't mean that there isn't an iceberg underneath.

提交回复
热议问题