Javascript private methods — what is the memory impact?

你说的曾经没有我的故事 提交于 2019-12-05 23:13:47

Okay, so from what I can see, constructing the class using the closure case constructs new function objects for each method defined within the constructor, while the prototype assignment way creates a central function that is shared by all instances of the objects. The central instance is then interpreted per object for the proper instance variable references.

I'm guessing each function defined in the closure example refers back to the same stack frame.

Still, in my case, it's a lot more objects floating about.

The following link shows information about some tips for profiling javascript functions to see information about their performance:

http://ejohn.org/blog/function-call-profiling/

Check out these benchmarks. Although they are inheritance benchmarks, it should give you an idea of the memory impact since some of them uses closures, and some don't.

The fact that variables enclosed in a closure can be updated from outside the closure in JavaScript, would suggest that there only ever exists one copy of the variable. Which means that there's no reason why there should be any significant memory impact of using closures in JavaScript.

If the values of the variables were frozen once the closure was created, that would be a different story since it would mean that each closure would have to have a private copy of its enclosed variables.

All that being said, you should still perform the actual benchmarks to check :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!