测试“类”的不同模拟的 Javascript 基准测试
我读过的文章说:
使用
原型
将是最快的,因为声明的函数是共享的。更多详细信息在 this 中进行了解释文章其中与使用“improvisions”相比,利用 JS 原生原型将提高性能。闭包应该表现更差,因为每次创建它返回一组函数和变量的单独副本。
对象(函数)是一种闭包,但带有
this
。具有访问控制(公共/私人)。它们应该比闭包更好。对象文字(看起来像 JSON 的那种)就像对象一样,但没有隐私感。它们相当于JAVA中的静态方法。我没有文章可参考,我只是将其包含在内以进行比较。
我正在 使用 jsperf 测试 4 种方法的简单比较< /a> 在我继续我的项目之前构建“类”,我期待原型获胜。然而,闭包击败了他们。怎么会?这里有一些给予和索取吗?看不见的偏见?
我可能对 JS 并不陌生,但我对这些优化概念完全陌生,请耐心等待。我还在研究这些事情。
i have read articles that say:
using the
prototype
will be fastest since functions declared are shared. more detail was explained in this article where tapping JS native prototype will increase performance as compared to using 'improvisions'.closures should perform worse since each creation of it returns a separate copy of a set of functions and variables.
objects (functions) are sort of closures but with
this
. has access control (public/private). they're supposed to be better than closures.object literals (the one looking like JSON) act like objects but have no sense of privacy. they are comparable to static methods in JAVA. i have no article to refer, i just included this for comparison.
i was testing a simple comparison of the 4 methods using jsperf in building "Classes" before i proceed with my project and i was looking forward to prototypes winning. however, closures beat them hands down. how come? is there some give and take here? unseen bias?
i may not be new to JS, but i'm totally new to these optimization concepts, please bear with me. i'm still studying about these things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“关闭应该表现得更糟”并不是理所当然的。闭包导致每个对象获得自己的函数副本。但只要您有足够的内存,就不会导致任何真正的性能问题。如果有的话,闭包甚至可能更快,因为它们不必沿着原型链向上查找属性,它们始终位于对象的第一层。
闭包的真正缺点是内存使用,而不是速度。当创建大量对象时,它可能会成为一个问题。
您还必须考虑相关的运行时间。不同的 JavaScript 引擎会针对不同的情况进行优化,具体取决于他们认为最重要的是什么。
It's not a given that "closures should perform worse". Closures cause each object to get their own copy of the function. But as long as you have enough memory, that shouldn't cause any real performance issues. If anything, closures may even be faster because they don't have to go up the prototype chain to find the property, they are always at the first level right on the object.
The real downside to closures is memory usage, not speed. When creating a ton of objects, it can become a concern.
You also have to consider the runtime in question. Different JavaScript engines will optimize for different situations, depending on what they thought was most important.
不同之处在于,闭包是在作用域链上查找的,属性是在内部原型链上查找的。尽管它们本质上都是对象属性查找(一个使用激活对象,另一个使用普通对象),但可能只是您测试的 UA 对其中一个的优化程度高于另一个。
PS 在 IE 8 中,示例中的原型速度更快。
The difference is that closures are looked up on the scope chain, properties on the internal prototype chain. Even though they are both fundamentally object property lookups (one using activation objects, the other plain Objects), it may simply be that the UAs you've tested are optimised more for one than the other.
P.S. In IE 8, prototypes are faster in the example.