Search code examples
javascriptperformancefunctionobjectgarbage-collection

Performance of passing object as argument in javascript


Theoretical question, if for e.g. I have a big object called Order and it has a tons on props: strings, numbers, arrays, nested objects.

I have a function:

function removeShipment(order) {
    order.shipment.forEach(
        // remove shipment action
    );
}

Which mean I access only one prop (shipment), but send a big object.

From perspective of garbage collection and performance is there a difference, between pass Order and pass Order.shipment?

Because object passed by reference, and don't actually copy Order into variable.


Solution

  • Created a simple test here https://jsperf.com/passing-object-vs-passing-raw-value

    Test results:

    • in Chrome passing object is ~7% slower that passing raw value
    • in Firefox passing object is ~15% slower that passing raw value
    • in IE11 passing object is ~10% slower that passing raw value

    This is syntetic test for passing only one variable, so in other cases results may differ