在测试“类”的不同仿真中的Javascript基准 [英] Javascript benchmarks in testing different emulations of a "Class"

查看:149
本文介绍了在测试“类”的不同仿真中的Javascript基准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读过使用原型说明的文章:

i have read articles that say:


  • >将是最快的,因为声明的函数是共享的。更多详情在此文章,其中点击JS原生原型将提高与使用简易版相比的性能。

  • 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'.

closure应该表现更差,因为每次创建都会返回集合的单独副本

closures should perform worse since each creation of it returns a separate copy of a set of functions and variables.

对象(函数)是一种闭包,但 this 。有访问控制(公共/私有)。他们是应该比封闭更好。

objects (functions) are sort of closures but with this. has access control (public/private). they're supposed to be better than closures.

对象字面量(看起来像JSON)像对象一样,但没有隐私感。它们与JAVA中的静态方法相当。

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.

我是在构建类时使用jsperf 测试4种方法的简单比较我继续我的项目,我期待原型获胜。然而,关闭打败他们。怎么来的?是有一些给和在这里吗?看不见的偏见?

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?

我可能不是JS的新手,但我完全是新的这些优化概念,请与我一起。我仍然在研究这些事情。

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.

推荐答案

这不是一个给定的closures应该执行更糟糕。闭包导致每个对象获得自己的函数副本。但是只要你有足够的内存,这不应该导致任何真正的性能问题。如果有什么,闭包甚至可能更快,因为他们不必上原子链来找到属性,他们总是在对象的第一级。

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.

您还必须考虑有问题的运行时。不同的JavaScript引擎会针对不同的情况进行优化,具体取决于他们认为最重要的。

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.

这篇关于在测试“类”的不同仿真中的Javascript基准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆