最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass around large object as argument - Stack Overflow

programmeradmin2浏览0评论

General question:
Does it affect performance when a large object is passed as a parameter vs when a native variable is passed?

Case: I've written a component that manages Google Maps.
In each of the methods of the component, it requires passing in the Google Maps object since I don't want to set the map as a property on the component.

General question:
Does it affect performance when a large object is passed as a parameter vs when a native variable is passed?

Case: I've written a component that manages Google Maps.
In each of the methods of the component, it requires passing in the Google Maps object since I don't want to set the map as a property on the component.

Share Improve this question asked Jul 7, 2015 at 17:10 TraceTrace 18.9k21 gold badges96 silver badges170 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 16

When you pass an object as an argument to a function - the only thing that is copied is the handler of that object (that's the address in memory where the object is stored). The object itself doesn't get cloned, so there's no overhead when you pass a big object as an argument.

If you pass a string it does get cloned, so in that case the length of the string is a concern.

In JavaScript it is always pass-by-value. But when an object is passed, the value itself is a reference.

Therefore, performance would not be affected by passing the large object, because what is being passed is a value that is just a reference to the object.

The size of object doesn't affect performance as objects in javascript are passed as reference.

There is a slight performance dip, since the object's location is being called when you send it to your method, but the ease of programming a new component or finding a bug in the object far outweighs the ~1 millisecond cost.

发布评论

评论列表(0)

  1. 暂无评论