问题描述
我通常不喜欢微基准测试。但是这个结果非常有趣。
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中计算楼层的最快方式。 ~~ n
, n | n
, n& 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.
地板是否做了其他方法无法做到的重要事情?有没有理由使用它?
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中计算楼层的最慢方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!