本文介绍了为什么 Javascript 的 Math.floor 是在 Javascript 中计算 floor 的最慢方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常不喜欢微基准测试.但是这个有一个非常有趣的结果.
http://ernestdelgado.com/archive/benchmark-on-the-floor/

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

它表明 Math.floor 是在 Javascript 中计算地板的最慢方法.~~nn|nn&n 都更快.
这看起来非常令人震惊,因为我预计在当今现代浏览器中实现 Javascript 的人将是一些非常聪明的人.

It suggests that Math.floor is the SLOWEST way to calculate floor in Javascript. ~~n, n|n, n&n all being faster.
This seems pretty shocking as I would expect that people implementing Javascript in today's modern browsers would be some pretty smart people.

floor 是否做了其他方法无法做到的重要事情?有什么理由使用它吗?

Does floor do something important that the other methods fail to do? Is there any reason to use it?

推荐答案

它与现代浏览器无关.它与实施 ECMA 标准有关.即使有更快的方法,您也不能仅仅改变某个函数的执行方式.它可能会破坏现有代码.

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.

Math.Floor 必须考虑处理不同类型的许多不同场景.他们能否像您描述的那样通过走捷径来更快地制作不同的场景?也许他们可以,但这可能会打破其他情况.仅仅因为表面上的东西看起来很小,并不意味着下面没有冰山.

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.

这篇关于为什么 Javascript 的 Math.floor 是在 Javascript 中计算 floor 的最慢方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!