向函数传递更小的对象是否更有助于提升性能?
Great question—let’s unpack this by tying it back to JavaScript’s "pass-by-sharing" behavior and real-world performance implications.
First, a quick recap to align with what you already know: in JavaScript, when you pass an object to a function, you’re passing a reference to the same underlying object in memory. Unless you reassign the parameter directly inside the function (e.g., obj = {}), changes to the object’s properties will reflect in the outer scope.
Now, onto your core question about performance:
Reference size doesn’t depend on object size
JavaScript object references are fixed-size values (typically 64 bits in modern engines). Whether you pass a massive object with 100+ properties or a tiny sub-object (like a nested dictionary), you’re essentially passing the same-sized reference. The act of passing itself has identical performance cost either way—no savings here.Minimal (often negligible) difference in property lookup
If your function only needs access to properties in the sub-object, passing the sub-object directly lets you skip one level of property lookup. For example:- Passing the full object:
function processUser(bigObj) { console.log(bigObj.user.name); } - Passing the sub-object:
function processUser(userObj) { console.log(userObj.name); }
This saves one property access step, but the overhead of a single property lookup is extremely small. You’d only notice a difference if this code runs in a tight loop with millions of iterations—most business logic won’t hit this threshold.
- Passing the full object:
Readability and maintainability should take priority
In almost all cases, the clarity of your code matters more than these micro-performance gains. Passing the specific sub-object your function needs makes the function’s dependencies explicit, reduces coupling to the larger object’s structure, and makes your code easier to test and debug. This is a far bigger win than any tiny performance boost.Watch out for unnecessary object creation
If you’re creating a new derived object (e.g.,const subObj = { name: bigObj.user.name }) just to pass to the function, you’re adding the overhead of object allocation and copying properties—this will actually hurt performance compared to passing the original large object or its existing sub-reference. Only pass existing sub-objects (likebigObj.user) to avoid this.
Summary
Unless you’ve identified a clear performance bottleneck tied to property lookups or object references (via profiling), don’t optimize for this scenario. Prioritize writing clear, focused functions by passing the smallest relevant object (sub-dictionary) that fits the function’s responsibility—any performance difference will be negligible in most cases.
内容的提问来源于stack exchange,提问作者Curcuma_

